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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by