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

1 visualización (últimos 30 días)
Ananya Malik
Ananya Malik el 20 de Mzo. de 2018
Comentada: Ananya Malik el 20 de Mzo. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 20 de Mzo. de 2018
Editada: Stephen23 el 20 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by