How to switch two values in a matrix?

Hi. I have a matrix :
x = [ 6.5 7 23 24 25]
x =
6.5000 7.0000 23.0000 24.0000 25.0000
However, the numbers inside matrix x are not in the correct spots. I need to move some of them. In order to find out which ones to move, I have to look at another matrix which is called c and c equals to:
c = [ 1 2 3 4 5 ;
3 2 4 1 5]
c =
1 2 3 4 5
3 2 4 1 5
I want to write a code which can detect which columns in matrix c do not have the same numbers in its two rows. For example in matrix c the answer is 1st, 3rd, and 4th columns. Following is what I have done.
y = find(c(1,:)~=c(2,:));
Now this line of code helps me to find where in numbers do not match.
Now I want a code which can apply the findings of y to swap the values in matrix x.
In other words, the out put of the code that I am looking for should give me
J = blahblah(x);
J = [ 24 7 6.5 23 25];
This is very important and I have no clue how to do it.
Any help would be appreciated.

 Réponse acceptée

Roger Stafford
Roger Stafford le 16 Nov 2016
If I understand the method in your example correctly, then using your definition of x and c, the following should accomplish the necessary transformation using matlab's 'circshift' function:
y = find(c(:,1)~=c(2,:));
J = x;
J(y) = J(circshift(y,1));

9 commentaires

c = [ 1 2 3 4 5; 3 2 4 1 5];
x = [ 6.5 7 23 24 25];
y = find(c(1,:)~=c(2,:));
J = x;
J(y) = J(circshift(y,1));
Hi, unfortunately this does not work :/ because x is exactly like J
Roger Stafford
Roger Stafford le 16 Nov 2016
Are you quite sure of that Changoleon? On my computer J is changed from x by that last line of code which shifts the three values around. Please check your work again.
Hi Roger, Would you please copy and paste the following lines of code into your MATLAB?
x = [ 6.5 7 23 24 25];
c = [ 1 2 3 4 5 ; 3 2 4 1 5];
y = find(c(1,:)~=c(2,:));
J = x;
J(y) = J(circshift(y,1));
J
x
Now if you click run, the values of J and x will appear in command window and they are the same :/ what am I doing wrong? I also get an orange message in the command window saying:
Warning: CIRCSHIFT(X,K) with scalar K and where size(X,1)==1 will change behavior in future versions. To retain current behavior, use CIRCSHIFT(X,[K,0]) instead. > In Untitled2 (line 9)
>> x = [ 6.5 7 23 24 25];
c = [ 1 2 3 4 5 ; 3 2 4 1 5];
y = find(c(1,:)~=c(2,:));
J = x;
J(y) = J(circshift(y,1));
J
x
J =
24 7 6.5 23 25
x =
6.5 7 23 24 25
R2016b.
Which version are you using?
Changoleon
Changoleon le 17 Nov 2016
WoW it works for you. It could be the versions then. Mine is R2015b! I'll download the new version and let you know how it went. Thanks
Changoleon
Changoleon le 17 Nov 2016
Modifié(e) : Changoleon le 17 Nov 2016
Walter,
Hi, I just tried that in my Laptop which has a R2016a and still x and J are same. What else could be wrong? I have attached a screenshot of my code.
J(y) = J(circshift(y,[0, 1]));
@Changoleon. Try this:
y = find(c(:,1)~=c(2,:));
J = x;
J(y) = J(circshift(y,1,2));
The '2' specifies that shifting is along the 2nd dimension (along the row)
Changoleon
Changoleon le 17 Nov 2016
Thanks! You're the best

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 16 Nov 2016
If you have two indices, I, J, at which you need to swap, then
YourMatrix([I, J]) = YourMatrix([J, I]);
will do the swap.
If you indices are expressed through a row vector of length 2, y, then
YourMatrix(y) = YourMatrix(fliplr(y));
Use flipud if it is a row vector.
This will not work as well with length greater than 2: if your vector were an odd length then one of the elements would be set to itself.

2 commentaires

Changoleon
Changoleon le 16 Nov 2016
As you said the values that I want to change could be more than 2. Up to 75. The method should be all automated. How can I do that?
The question becomes how to match the source and destination.
The general syntax
YourMatrix(ListOfDestiantions) = Yourmatrix(ListOfCorrespodingSources);
reads everything from Yourmatrix(ListOfCorrespodingSources) before doing any assignments at YourMatrix(ListOfDestiantions), so it is safer than using
for K = 1 : length(ListOfCorrespodingSources)
YourMatrix(ListOfDestinations(K)) = YourMatrix(ListOfCorrespodingSources(K));
end
The "for" version of it messes up on swapping values.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by