i want to find row indices for each column having non zero values

i have a matrix a=[1 1 0 1;1 1 0 0;0 0 1 1] i want row indices for each column having non zero elements as [1,2] [1,2] [3] [1,3]

 채택된 답변

Stephen23
Stephen23 2016년 3월 25일
편집: Stephen23 2016년 3월 25일
An easy way using accumarray:
>> a = [1,1,0,1;1,1,0,0;0,0,1,1]
a =
1 1 0 1
1 1 0 0
0 0 1 1
>> [row,col] = find(a);
>> C = accumarray(col,row,[],@(r){r});
>> C{:}
ans =
1
2
ans =
1
2
ans =
3
ans =
1
3

추가 답변 (1개)

Jos (10584)
Jos (10584) 2016년 3월 25일
A = [1 1 0 1; 1 1 0 0; 0 0 1 1]
C = arrayfun(@(k) find(A(:,k)),1:size(A,2),'un',0)
% C{x} holds the row numbers for which A(:,x) is non-zero

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2016년 3월 25일

편집:

2016년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by