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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by