Integrating Indices

Hello,
I have taken the matrix below and sorted it ascending by row:
(Original) A=[1 4 5; 3 -1 8; 12 7 9; 4 10 -5];
(Sorted By Row) B=[1 4 5; -1 3 8; 7 9 12; -5 4 10];
I would like to have the indices of the original matrix appear in the locations of the sorted matrix. For this case it would look like this.
(Original) A=[1 2 3; 1 2 3; 1 2 3; 1 2 3];
(Sorted By Row) B=[1 2 3; 2 1 3; 2 3 1; 3 1 2];
I have tried several different methods but with no luck. Any assistance is appreciated
Thanks

댓글 수: 1

zohar
zohar 2011년 2월 21일
Hi Daniel
Give us some code and use the code formatting.

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

 채택된 답변

Walter Roberson
Walter Roberson 2011년 2월 21일

2 개 추천

[sorted_A, sort_indexes] = sort(A,2);

댓글 수: 2

Daniel
Daniel 2011년 2월 21일
Thank you, it still isn't quite what I need.I may have worded the question wrong.
Oleg Komarov
Oleg Komarov 2011년 2월 21일
All of your examples are solved with Walter's solution!

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

추가 답변 (2개)

Daniel
Daniel 2011년 2월 21일

0 개 추천

A=[ 4 3; 8 4;-1 -5; 2 7];
A1=A
[m,n]=size(A);
for o=1:n
A1=A;
min_val=A(1);
for i=1:n-1
for j=1:m
if A(j,i)>A(j,i+1);
A(j,i+1)=A(j,i);
A(j,i)=A1(j,i+1);
end
end
end
end
A
comp=isreal(A)
if comp==0
A2=abs(A);
else
A2=A;
end
This is what I currently have. The first for loops sort the matrix by row. The second section ensures a real answer.
Daniel
Daniel 2011년 2월 21일

0 개 추천

Yes I have tried Walter's solution. When added to my code it yeilds the following.
sort_indexes =
1 2
1 2
1 2
1 2
For these two matrices, it should read:
sort_indexes =
2 1
2 1
2 1
1 2
this is much closer to what I need, but if it worked properly it would display the indexes of the first matrix in the positions in the second matrix. Is there any way to obtain this solution?

댓글 수: 2

Oleg Komarov
Oleg Komarov 2011년 2월 21일
A=[ 4 3; 8 4;-1 -5; 2 7];
[sorted_A, sort_indexes] = sort(A,2)
gives exactly the second matrix...
Daniel
Daniel 2011년 2월 21일
I apologize, something was wrong with the way that I had input this solution. Both you and Walter are Correct.
Thank you

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

카테고리

도움말 센터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!

Translated by