For each column of matrix A consisting of "0" and "1", I would like to find the column indices of the first occurrence of "1" if one exists. For example, if A is defined as:
A=[0 0 0 0; 0 0 0 1; 0 0 0 0; 0 0 0 1; 1 0 0 0; 0 1 0 1; 1 1 0 0]
then the result will be:
b=[5 6 2]
I am looking for a solution without any 'for' or 'while' loops.
In one solution, I came up with:
[b,~]=find(cumsum(cumsum(A))==1)
Is there a more elegant way to do this?
source share