Wednesday, April 15, 2020

find vs logical indexing

people usually use "find" to get some values with a threshold, but the logically indexing is much easier and faster. This is the lesson I learned today: https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html https://www.mathworks.com/matlabcentral/answers/66992-logical-indexing-is-usually-faster-than-find n = 1e6; r = rand(n,1); f = randn(n,1)+10; si = r>0.5; tic j = find(si); s1 = f(j); % with FIND toc tic s2 = f(si); % with logical indexing toc all(s1==s2) This is the best example I found, I usually used s1 method, but s2 is faster, if you dont' need the actual index number

No comments: