Find out non-zero indexes of rows in matrix

2 Ansichten (letzte 30 Tage)
lucksBi
lucksBi am 10 Jun. 2017
Kommentiert: lucksBi am 10 Jun. 2017
hey
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.
Thanks

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by