Simple Matrix Reduction Question. Ones and Zeros.

2 次查看(过去 30 天)
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