Created
August 17, 2013 15:34
-
-
Save tuix/6257447 to your computer and use it in GitHub Desktop.
Benchmarking answers to question 18282458 in stackoverflow
http://stackoverflow.com/questions/18282458/how-to-count-length-of-runs-of-values-in-matrix-columns
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In MATLAB 2012b (64-bit) on Windows 7, it returns
A ratio of 7.7 in execution time.