Using find() is faster than direct logical indexing?
Show older comments
Does it make sense that using find() instead of direct logical indexing is faster in this example:
A=randi(100,5e3,5e3);
timeit(@()useFind(A))
timeit(@()useLogical(A))
function B=useFind(A)
I=find(A==2);
B=A;
B(I)=3;
end
function B=useLogical(A)
B=A;
B(A==2)=3;
end
4 Comments
Bruno Luong
on 3 Aug 2022
Can't reproduce your result on my Laptop
ans =
0.0731 % find
ans =
0.0693 % logical
dpb
on 3 Aug 2022
I'd contend the difference isn't significant to begin with...
Adam
on 3 Aug 2022
I get similar results, albeit that my PC is about 2½ times slower! Find is typically around 8% slower for me on this example.
Bruno Luong
on 3 Aug 2022
Wonder why I get faster than you guys, my PC is just a normal PC, I don't even have Nvidia card and I'm not a gammer (well if one consider MATLAB is not a game).
Accepted Answer
More Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!