Skip to content

Instantly share code, notes, and snippets.

@tuix
Created August 17, 2013 15:34
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 tuix/6257447 to your computer and use it in GitHub Desktop.
Save tuix/6257447 to your computer and use it in GitHub Desktop.
d = [0 1 1 1 1;
1 1 0 0 0;
1 1 1 0 1;
0 0 0 0 0;
1 1 0 1 1];
tic
for k=1:1e4,
nrows = size(d,1);
d_neg_cell = num2cell(~d,[nrows 1]);
zeros_d = cellfun(@find, d_neg_cell, 'UniformOutput', 0);
find_runs = @(v) nonzeros( diff([0; v; nrows+1])-1 ).';
sol = cellfun(find_runs, zeros_d, 'UniformOutput', 0);
end
toc
tic
for k=1:1e4
v = {};
for e = d,
f = diff([0 e' 0]);
v{end+1} = find(f<0) - find(f>0);
end
end
toc
@tuix
Copy link
Author

tuix commented Aug 17, 2013

In MATLAB 2012b (64-bit) on Windows 7, it returns

Elapsed time is 4.131100 seconds.
Elapsed time is 0.534464 seconds.

A ratio of 7.7 in execution time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment