I want to interchange matrix.
A = [ 1 5 5 2 5 3 5 4 9 7 8 6 6 4];
i want it to interchange with with last unique value.
A = [1 5 5 2 5 3 5 4 9 7 8 6 4 6]; % Means A(14) replace with A(13).
then search next unique value and interchange with A(11) because 4, 6 values already interchanged. Please help me.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 25일

0 개 추천

A([13 14]) = A([14 13]);
However, neither 4 nor 6 are unique in A, so I do not understand why you are exchanging them.
Once you have exchanged 4 and 6, then the last unique value is 8, which is already at A(11), the position you say it needs to end up in anyhow. What should it be exchanged with?

댓글 수: 2

Triveni
Triveni 2016년 1월 25일
편집: Triveni 2016년 1월 25일
My question is not just interchange values?... I want program that interchange values one by one....By loop
First you need to identify the rule for doing the exchange.
Then you program your loop to identify the locations to exchange. If you want to exchange positions P and Q then you use
A([P Q]) = A([Q P]);
I do not know what rule you are using, as the example you give does not agree with your explanation of what needs to be done. The last unique value is the 8 in position #11, not the 6 in position #13. If we exchange that 8 with what is in the last position, then the next to last unique value is the 7 in position #10. Would that go to the right of the last place you exchanged to, which is to position #13? Or would it go to the right of where you found the unique value? Or would it go somewhere else? What is to be done if the last unique value is already in the position you would exchange with?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

태그

질문:

2016년 1월 25일

댓글:

2016년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by