Fastest way to AND or OR together columns of a matrix
2 views (last 30 days)
Show older comments
Adrian Mariano
on 4 Feb 2019
Commented: Adrian Mariano
on 5 Feb 2019
I'm using 2017b and I need to test whether each row of a matrix meets an inequality constraint. This test is done millions of times in my code, so I'm trying to make it as fast as possible. I'm wondering if there is any faster way to do it than this
testdata = rand(1000,26); % setup some fake data
testref = rand(1,26)/20; % setup a fake test reference
result = sum(testdata >= testref,2)==26; % Actual test; can this be faster?
0 Comments
Accepted Answer
Fangjun Jiang
on 4 Feb 2019
Edited: Fangjun Jiang
on 4 Feb 2019
try
result = all(testdata >= testref,2)
It is quite faster.
Elapsed time is 0.000445 seconds.
Elapsed time is 0.000294 seconds.
Elapsed time is 0.000426 seconds.
Elapsed time is 0.000254 seconds.
Elapsed time is 0.000406 seconds.
Elapsed time is 0.000255 seconds.
7 Comments
Matt J
on 5 Feb 2019
Edited: Matt J
on 5 Feb 2019
But the question remains: what's wrong with testing run time at the command line?
Whatever optimizations Matlab does, it only does them in function files, so command line execution is never the reliable way to see the best that Matlab can do.
Is the conclusion about my original question that using sum() is the fastest method?
We certainly can't conclude that in general. As you can see from my timing tests above, I found all() to be faster, which makes sense because it involves fewer function calls. Possibly, though, you have a very strange computer...
More Answers (1)
Fangjun Jiang
on 5 Feb 2019
It looks like depending on the size of the test data. If the data has 1e6 rows or less, sum() is faster. If the data has 1e7 rows or more, all() is faster.
rng(1);
testdata = rand(1e6,26); % setup some fake data
testref = rand(1,26)/20; % setup a fake test reference
cmp=(testdata >= testref);
tic;
result = sum(cmp,2)==26;
t_sum=toc;
tic
result = all(cmp,2);
t_all=toc;
if t_sum<t_all
fprintf('sum() is faster\n\n');
else
fprintf('all() is faster\n\n');
end
7 Comments
Fangjun Jiang
on 5 Feb 2019
I did try cputime() but didn't find difference when running it once (both are zero). You could try it if you really want to find it out. Again, I thought the choice for using all() was clear but tic/toc indicated otherwise. I guess there are still some explanation needed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!