Simple Matrix Reduction Question. Ones and Zeros.

조회 수: 2 (최근 30일)
Chad
Chad 2020년 4월 1일
답변: Stephen23 2020년 4월 1일
I have a NxM matrix full of ones and zeros. Each row (1:N) has exactly one, one, somewhere along of the columns. I want to make a matrix Nx1 which shows which columns the ones appeared in. Example:
A = [ 0 0 0 1; 1 0 0 0; 0 1 0 0; 0 0 1 0]
B = [ 4 ; 1 ; 2 ; 3 ]
Should be very simple?

채택된 답변

Birdman
Birdman 2020년 4월 1일
편집: Birdman 2020년 4월 1일
[rows,cols]=find(A==1);
[C,I]=sort(rows);
B=cols(I)

추가 답변 (1개)

Stephen23
Stephen23 2020년 4월 1일
Simpler:
>> [B,~] = find(A.')
B =
4
1
2
3

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by