How to interchange values in matrix

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 25 Gen 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 Commenti
Triveni
Triveni il 25 Gen 2016
Modificato: Triveni il 25 Gen 2016
My question is not just interchange values?... I want program that interchange values one by one....By loop
Walter Roberson
Walter Roberson il 25 Gen 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?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by