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

1 Ansicht (letzte 30 Tage)
Ananya Malik
Ananya Malik am 20 Mär. 2018
Kommentiert: Ananya Malik am 20 Mär. 2018
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Mär. 2018
Bearbeitet: Stephen23 am 20 Mär. 2018
>> 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

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by