How to interchange values in matrix

1 次查看(过去 30 天)
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
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
Walter Roberson
Walter Roberson 2016-1-25
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 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by