You can use max
:
>> [sel, c] = max( A ~=0, [], 2 );
Rows for which sel
equalse zero - are all zeros and the corresponding column in c
should be ignored.
Result:
>> [sel c]= max( A~=0, [], 2 )
sel =
1
1
1
1
1
c =
1
1
3
2
1
In order to find the first non-zero row index (for each column) you just need to apply max
on the first dimension:
>> [sel r] = max( A~=0, [], 1 );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…