Find out non-zero indexes of rows in matrix

조회 수: 1 (최근 30일)
lucksBi
lucksBi 2017년 6월 10일
댓글: lucksBi 2017년 6월 10일
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

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 10일
[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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by