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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Stephen23
Stephen23 il 20 Mar 2018
Modificato: Stephen23 il 20 Mar 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

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by