For example,
A = [ -1 0 -2 0 0
2 8 0 1 0
0 0 3 0 -2
0 -3 2 0 0
1 2 0 0 -4];
how can I get a vector of the first nonzero elements of each row?
For example,
A = [ -1 0 -2 0 0
2 8 0 1 0
0 0 3 0 -2
0 -3 2 0 0
1 2 0 0 -4];
how can I get a vector of the first nonzero elements of each row?
You can use
max
:Rows for which
sel
equalse zero - are all zeros and the corresponding column inc
should be ignored.Result:
In order to find the first non-zero row index (for each column) you just need to apply
max
on the first dimension:Here is a solution based on accumarray that will work even if a row is all zeros.
You can do it by executing find function for each row as follows: