reverse indexing of a matrix in matlab

조회 수: 95 (최근 30일)
lilly lord
lilly lord 2020년 6월 7일
댓글: lilly lord 2020년 6월 7일
Hi I have the code and i want the inverse mapping that is to get back P
P=[188 5 95 60;3 59 0 111;255 123 51 84 ];
Ma=[25 222 80 6;100 1 190 97;73 33 254 184 ];
M_1=zeros(size(Ma ));
P_1=zeros(size(P ));
for i=1:3
[M_1(i,:),Index]=sort(Ma(i ,:));
P_1(i,:) = P(i,Index );
end
%%%%%%Inverse
m_1=zeros(size(M_1));
p_1=zeros(size(P_1));
for j=1:3
[m_1(j,:),Index]=Ma(j,:);% here I dont need any sort commant bur error occurs here
p_1(j,:)=P_1(j,Index);
end
p_1
  댓글 수: 1
KSSV
KSSV 2020년 6월 7일
[m_1(j,:),Index]=Ma(j,:);
This will not work.....from where you expect to get Index? What actualy you are looking for.

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

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 6월 7일
Hi Lilly,
You get error at that line, because, you just removed the sort operation, which can provide two outputs.
The code can not have two outputs, when it is just a variable and no operation is performed on it.
Your code above doesn't perform what you wanted to do. You can perform or get the inverse, with the help of Index variable.
Here is the code that can be done, if you have Index variable
P=[188 5 95 60;3 59 0 111;255 123 51 84 ];
Ma=[25 222 80 6;100 1 190 97;73 33 254 184 ];
M_1=zeros(size(Ma ));
P_1=zeros(size(P ));
Index = zeros(size(Ma));
for i=1:3
[M_1(i,:),Index(i,:)]=sort(Ma(i ,:));
P_1(i,:) = P(i,Index(i,:) );
end
%%%%%%Inverse
% Perform this with the help of Index
p_1 = zeros(size(P_1));
for j = 1:3
p_1(j,Index(j,:)) = P_1(j,:);
end
p_1
isequal(p_1,P) % To check if p_1 is equal to P
Hope this helps.
Regards,
Sriram
  댓글 수: 3
Sriram Tadavarty
Sriram Tadavarty 2020년 6월 7일
Hi Lilly,
Did you try the above code as is? I do get the output. Can you once clear your workspace and try?
I just didn't change only the second for loop. There is even a change in the first for loop to store the index values. Please do let me know, once you try the code as is.
Hope this helps.
Regards,
Sriram
lilly lord
lilly lord 2020년 6월 7일
Oh sorry, i have changed the bottom loop only. Now I get the correct output. Thank you so much

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by