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.
source share