Find out non-zero indexes of rows in matrix

2 views (last 30 days)
how can i find out indexes of non-zero elements of all rows in a matrix.e.g.
matrix= [1,0,0,2,0,0;1,2,6,5,0,3;1,0,2,9,4,0]
it'll show 1;3 for row 1, 1;2;3;4;6 for row2 and 1;3;4;5 for row3.
And also instead of doing for whole matrix if i ask for a specific row e.g. row 1 then it'll show 1;3 only.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2017
[c, r] = find(matrix.');
indices = accumarray(r, c, [size(matrix,1), 1], @(L) {L.'} );
now indices{K} will be the list of non-zero columns in row #K

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!