Matlab Exchange

I am trying to create a function that will replace a specific number in a matrix with a specific number in the same matrix. For example, if I start with A = [1 2 3; 1 3 2], I want to be able to create B = [2 1 3; 2 3 1], simply by indicating Matlab to replace 1 with 2. Any advice would be taken into account. Thanks!

+4
source share
6 answers

If you have the following matrix:

A = [1 2 3; 1 3 2]; 

and you want all those who become two, and two to become such, the following way would be the easiest way:

 B = A; B(find(A == 1)) = 2; B(find(A == 2)) = 1; 

EDIT:

As Kenny suggested, this can be further simplified because:

 B = A; B(A == 1) = 2; B(A == 2) = 1; 
+3
source

Another way to handle the original task is to create a permutation vector indicating which numbers the source records should be mapped to. For example, the entries [1 2 3] should be displayed respectively in [2 1 3], so that we can write

 A = [1 2 3; 1 3 2]; perm = [2 1 3]; B = perm(A) 

(the advantage here is that everything is done in one step and that it also works for operations more complex than swaps; the disadvantage is that all elements of A must be positive integers with a known maximum)

+3
source

Not sure why you should have done this special swap (row and column exchanges are more common). Matlab often means ":" to represent everything. Here's how to swap rows and columns:

To replace strings:

 A = A([New order of rows,,...], :) 

To replace columns:

 A = A(:, [New order of columns,,...]) 

To change the entire i-th column:

 A(:, i) = [New; values; for; i-th; column] 

For example, to replace the 2nd and 3rd columns, A = [1 2 3; 1 3 2]

 A = A(:, [1, 3, 2]) 
+2
source
 A = [1 2 3; 1 3 2] alpha = 1; beta = 2; indAlpha = (A == alpha); indBeta = (A == beta); A(indAlpha) = beta; A(indBeta ) = alpha 

I like this decision, it makes it clear what is happening. Less magic numbers can easily be turned into a function. Recycle the same matrix, if important.

+2
source

I don't have a copy of MatLab, but I think you can do something like this:

 for i=1:length(A) if (A(i)=1), B(i) = 2, B(i)=A(i) end 

Note that only convert 1 to 2, and it looks like you also want to convert 2 to 1, so you will need to do a little work.

There's also probably a much more elegant way to do this, given that you can do such things in Matlab

 >> A = 1:1:3 A = [1,2,3] >> B = A * 2 B = [2,4,6] 

There may be a swapif primitive that you can use, but I have not used Matlab for a long time, so I'm not sure if this is the best way to do this.

+1
source

Regarding tarn's a more elegant way of exchanging values, you can use the permutation matrix as follows:

 >> a =[1 2 3]; >> T = [1 0 0; 0 0 1; 0 1 0]; >> b = a*T ans = 1 3 2 

but this will replace column 2 and column 3 of the vector (matrix) a; while the question asked about the exchange of 1 and 2.

Update

To swap elements of two different values, look at the search function

ind = find (a == 1);

returns the indices of all elements with a value of 1. Then you can use the Mitch clause to change the value of elements using index arrays. Remember that find returns a linear index into a matrix; the first element has index 1, and the last element of nxm has linear index n * m. The linear index is counted in columns. for instance

 >> b = [1 3 5;2 4 6]; >> b(3) % same as b(1,2) ans = 3 >> b(5) % same as b(1,3) ans = 5 >> b(6) % same as b(2,3) ans = 6 
+1
source

All Articles