Create a gist now

Instantly share code, notes, and snippets.

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
@tuix
Owner

This returns:

Elapsed time is 0.134269 seconds.
Elapsed time is 0.672458 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment