필터 지우기
필터 지우기

How to rearrange a matrix in a preferred order

조회 수: 11 (최근 30일)
Homayoon
Homayoon 2016년 5월 10일
편집: Stephen23 2016년 5월 10일
Dear All,
I have a matrix of 40 *3 that is originally is in the numeric order with respect to the first column i.e.
[1 100 234
2 345 890
3 455 111
.
.
.
40 444 555]
However, I would like to rearrange the original matrix based on the following arbitrarily guideline:
[35
20
2
4
1
.
.
39]
Thus, my desired output is a new 40*3 matrix whose first, second and third (and etc) rows are the 35th, 20th and second (and etc) rows of the original matrix. Is there a possible way to rearrange any matrix in the user-defined way as I did in my example above?
Thanks.

채택된 답변

Stephen23
Stephen23 2016년 5월 10일
편집: Stephen23 2016년 5월 10일
Some basic MATLAB indexing does the trick:
>> old = randi(9,5,3);
>> old(:,1) = 1:5
old =
1 6 4
2 8 9
3 9 6
4 1 2
5 2 5
>> idx = [2,3,5,1,4];
>> new = old(idx,:)
new =
2 8 9
3 9 6
5 2 5
1 6 4
4 1 2
Using indexing is a very basic MATLAB concept. To learn this kind of basic usage all beginners should do these tutorials:
  댓글 수: 2
Homayoon
Homayoon 2016년 5월 10일
편집: Stephen23 2016년 5월 10일
Thank you so much for the prompt reply. I really appreciate it, Might I ask a more general question? Assuming that the original matrix is 160*4 and the first column elements are 1 to 160,what if I want to rearrange this matrix using the arbitrary guideline indicated in the body of my question? In this case the desired output has to be:
[35 .. ..
20 .. ..
2 .. ..
4 .. ..
1 .. ..
.
.
39 .. .. %the first 40 rows
75 .. ..
60 .. ..
42 .. ..
.
.
]
as you can see the big matrix rearranged in the group of 40 rows based on the guideline matrix. Is there any general answer?
Stephen23
Stephen23 2016년 5월 10일
편집: Stephen23 2016년 5월 10일
My answer shows how to rearrange your matrix based on some indices (you call the indices "arbitrary guideline", but in reality they are simply indices):
yourMatrix(theIndices,:)
That is all. Have you tried this yet?
Now you have given some new explanation or requirements, but not very clearly. What does "as you can see the big matrix rearranged in the group of 40 rows based on the guideline matrix" mean ?
How is the output in your comment different to what you asked in your original question?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by