Vectorized FIND with 'FIRST' option

version 1.1.0.0 (5.13 KB) by Bruno Luong
Find the first non-zero element(s) along row/column - or any other dimension

2.9K Downloads

Updated Wed, 19 May 2010 02:44:18 +0000

View License

Up to now, to find the first non-zero element of a matrix along a column (for example) user might:

1. Use for-loop with FIND command
for j=1:size(A,2)
... = find(A(:,j), 1, 'first');
end

2. Use other vectorized methods, often time not very straight-forwards (MAX, LOGICAL, SPARSE, etc...). This method creates temporary arrays and scan such the arrays few times while doing some calculation.

3. Write the basic FOR-LOOP algorithm to accomplish the task.

The FINDFIRST command does just the same, but implemented using MEX engine which should be faster than any of the above. It can handle 'FIRST', 'LAST' option, and return more than one FIND count.

NOTE: not yet tested on Linux (gcc)

Cite As

Bruno Luong (2022). Vectorized FIND with 'FIRST' option (https://www.mathworks.com/matlabcentral/fileexchange/24641-vectorized-find-with-first-option), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired: csearch

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!