Sort matrix in specific order

조회 수: 10 (최근 30일)
letizia533
letizia533 2018년 2월 20일
댓글: Walter Roberson 2018년 2월 20일
Hello everyone,
I have created a 288:4 matrix in which the third column consists of the numbers 1,2 and 3. I would like to sort the matrix so that each number in column three is repeated 6 times for 16 times in total while also making sure that
  • there will be no 6-digit-blocks of the same number twice in a row (i.e. 1 1 1 1 1 1 ; 2 2 2 2 2 2 ; 1 1 1 1 1 1 and NOT 1 1 1 1 1 1 ; 1 1 1 1 1 1 ; 2 2 2 2 2 2)
  • each column is moved with the corresponding content of the other columns in that row (this is why I cannot just create a new matrix with the order I need and replace it with the third column)
I would really appreciate your help!
Thanks a lot in advance Letizia
  댓글 수: 1
Image Analyst
Image Analyst 2018년 2월 20일
"I have created a 288:4 matrix" Please attach this matrix in a .mat file with the paper clip icon. Also, you have not actually asked an actual question, just made an announcement. Do you have a question?
You say "each column is moved with the corresponding content of the other columns in that row" Well, I have no idea what that means. Tell me again exactly where do you want to move the column? Or do you just want to move all rows together, in synchrony, so that each column is sorted identically to all the other columns?

댓글을 달려면 로그인하십시오.

답변 (1개)

letizia533
letizia533 2018년 2월 20일
Thanks for your quick response and sorry for being too imprecise. My question is if there is any way I can sort the matrix in that specific order or rather which functions I need to incorporate to solve this problem?
And concerning your next question: maybe I can explain it better with an example.
So if A =
A K 2 D
F D 3 F
W T 2 G
I want to make sure that when I change the order of the numbers in column three I move entire rows and not just switch the numbers in that column so that I get
A2 =
A K 2 D
W T 2 G
F D 3 F
and not A =
A K 2 D
F D 2 F
W T 3 G
Do you understand what I mean?
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 2월 20일
Generally speaking
[~, sort_order] = sort(A(:,appropriate_column_number));
A2 = A(sort_order, :);

댓글을 달려면 로그인하십시오.

카테고리

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