MATLAB: Find function

find

Hello,
can anyone enlighten me about the use of find() function? Is it really so bad in performance to use? Any alternative?
Thanks, Chiara

Best Answer

No. I'm guessing you are getting feedback from either humans or the MATLAB Code Analyzer about using logical indexing instead of find...? In that case, yes, that's true, but it depends on your application. If you really need indices, then use find. If, however, you're only using it to index into other arrays, skip find and just index directly. Eg instead of
idx = find(x > pi);
z = y(idx);
do
idx = x > pi;
z = y(idx);
Similarly, if you just want to know how many values satisfy some criterion, use nnz rather than numel(find(...))
Bottom line: there's nothing wrong with find if you really want to find indices. But if it's really just an intermediate step to something else, there are often neater ways to get there.