How to interchange values in matrix
2 views (last 30 days)
Show older comments
Triveni
on 25 Jan 2016
Commented: Walter Roberson
on 25 Jan 2016
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.
0 Comments
Accepted Answer
Walter Roberson
on 25 Jan 2016
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 Comments
Walter Roberson
on 25 Jan 2016
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?
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!