t = matrix==-1;
[~, pos] = max(t,[],2); %// position of first -1 within each row.
pos = pos .* any(t,2); %// give 0 for rows that don't contain any -1 value
pos(k)
gives the position of the (first) -1
value within row k
, or 0
if there isn't any -1
value in that row.
Example:
matrix =
1 0 0 1
0 1 -1 1
0 1 1 -1
0 0 1 -1
gives
pos =
0
3
4
4