Keeping track of order of rows when sorting a matrix
    조회 수: 10 (최근 30일)
  
       이전 댓글 표시
    
I am trying to sort the matrix B below and also keep track of the order in which rows get swapped. 
B = [100000001
    010000100
    001010010
    010001100
    001000010
    000001100
    100100000
    001010000]; 
B = -sortrows(-B); 
To keep track of each row number, can you for example add the row number column at the beginning, then sort the matrix according to all columns except the first? I am not sure how, because sortrows considers all columns. Or other than this is there any better method? 
B = [1100000001
    2010000100
    3001010010
    4010001100
    5001000010
    6000001100
    7100100000
    8001010000]; 
B = -sortrows(-B); 
댓글 수: 0
채택된 답변
  Stephen23
      
      
 2021년 11월 8일
        
      편집: Stephen23
      
      
 2021년 11월 8일
  
      "Or other than this is there any better method? "
The MATLAB approach is to get the second output from SORTROWS, which is the sort index, e.g.:
[B,X] = sortrows(-B);
B = -B;
which could be simplified using SORTROWS optional direction argument:
[B,X] = sortrows(B,'descend');
"I am not sure how, because sortrows considers all columns."
Did you read the SORTROWS help? It explains the options that let you select which columns you want to sort by.
Reading the documentation is the best way to learn what MATLAB functions can do.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

