MATLAB: Find the First element that satisfies a condition

find

I have an nxn matrix and would like to find the first element in each row that satisfies a given condition, and gives me NaN if there are none that meet the condition. The find command will give me all elements that satisfy it, but I only care about the first one in each row.
For instance, if my matrix is A = [7 3 9;5 8 8;0,2,6] and my condition is A<5, I would like to use a function that gives me [3 NaN 0] or at least tells me where in the matrix I can find those values and use it to index A.
Thanks!

Best Answer

No loops or find is required, just some logical indexing does the trick:
>> A = [7 3 9;5 8 8;0,2,6];
>> B = A.'; % important: arrange the data columnwise
>> X = B<5;
>> Y = cumsum(X,1)==1 & X;
>> Z = any(Y,1);
>> out(Z) = B(Y);
>> out(~Z) = NaN
out =
3 NaN 0