Skip to content

Instantly share code, notes, and snippets.

/so.m

Created August 21, 2015 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d630d108a5c684fff264 to your computer and use it in GitHub Desktop.
Save anonymous/d630d108a5c684fff264 to your computer and use it in GitHub Desktop.
A = round(1+499.*rand(1,1000));
B = round(501+499.*rand(1,1000));
t = zeros(1000,3);
for k=1:300
a = A(1:k);
b = B(1:k);
% dasdingonesin
tic;
c = cell2mat(arrayfun(@(x,y) x:y,a,b,'UniformOutput',false)); %#ok<NASGU>
t(k,1) = toc;
% for loop
c = [];
tic;
for m=1:length(a);
c = [c, a(m):b(m)]; %#ok<AGROW>
end;
t(k,2) = toc;
% Luis Mendo
tic;
m = (0:max(b-a)).';
c = bsxfun(@plus, a, m);
mask = bsxfun(@le, m, b-a);
c = c(mask).';
t(k,3) = toc;
end
x = 1:k;
plot(x,t(x,1),x,t(x,2),x,t(x,3));
legend('dasdingonesin','for loop','Luis Mendo');
xlabel('Array length');
ylabel('Time [s]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment