MATLAB: Apply Find to each row of an array

findMATLAB

I have an array of dimension [X Y], and I want to be able to efficiently apply the FIND function to each row of the array in order to find the location of the first non-zero index in each row.
By 'efficiently', I mean that I want to do so without having to go through a FOR loop – can I do so?
I understand there is a function ROWFUN, but I believe I would have to create a separate function that includes FIND in order to use that.
Thanks for any help.

Best Answer

Don't. That is not going to be efficient.
idx_first_nonzero = sum( cumprod(YourArray == 0, 2), 2) + 1;
Note: if any row is all-zero, this will return 1 longer than the array width. You have not defined a desired result for that case.