Created
August 13, 2013 07:16
-
-
Save tuix/6218612 to your computer and use it in GitHub Desktop.
Code used to benchmark solutions in http://stackoverflow.com/questions/18201729/forcing-the-columns-of-a-matrix-within-different-limits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L = randn(20,3); | |
l1_max=20; l1_min=0.5; | |
l2_max=20; l2_min=0.5; | |
mu_max=20; mu_min=0.5; | |
tic | |
for kk=1:1e5 | |
l=L; | |
for k=1:20 | |
if l(k,1)>l1_max | |
l(k,1) = l1_max; | |
elseif l(k,1)<l1_min | |
l(k,1) = l1_min; | |
end | |
if l(k,2)>l2_max | |
l(k,2) = l2_max; | |
elseif l(k,2)<l2_min | |
l(k,2) = l2_min; | |
end | |
if l(k,3)>mu_max | |
l(k,3) = mu_max; | |
elseif l(k,3)<mu_min | |
l(k,3) = mu_min; | |
end | |
end | |
end | |
toc | |
tic | |
for kk=1:1e5 | |
l=L; | |
l(l(:, 1) > l1_max, 1) = l1_max; | |
l(l(:, 1) < l1_min, 1) = l1_min; | |
l(l(:, 2) > l2_max, 2) = l2_max; | |
l(l(:, 2) < l2_min, 2) = l2_min; | |
l(l(:, 3) > mu_max, 3) = mu_max; | |
l(l(:, 3) < mu_min, 3) = mu_min; | |
end | |
toc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This returns: