Sort rows based on number of 1's in each row.

1 回表示 (過去 30 日間)
Ananya Malik
Ananya Malik 2018 年 3 月 20 日
コメント済み: Ananya Malik 2018 年 3 月 20 日
I have a matrix A = [1 1 0 1; 1 0 0 0 ; 1 0 1 0 ; 1 1 1 1]; I count the number of 1's in each row and I get count=[3 1 2 4]. I want to get A' = [1 1 1 1; 1 1 0 1; 1 0 1 0; 1 0 0 0]; How to get it. TIA.

採用された回答

Stephen23
Stephen23 2018 年 3 月 20 日
編集済み: Stephen23 2018 年 3 月 20 日
>> A = [1,1,0,1;1,0,0,0;1,0,1,0;1,1,1,1];
>> [~,idx] = sort(sum(A,2),'descend');
>> A(idx,:)
ans =
1 1 1 1
1 1 0 1
1 0 1 0
1 0 0 0
  1 件のコメント
Ananya Malik
Ananya Malik 2018 年 3 月 20 日
Thanks a lot (y).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by