Substituting particular matrix values

I have four 512x512 matrices: X1a and X1b, X2a and X2b
I first want to find the coordinates in X1a and X2a where an element equals 255. For this I have been using:
[r,c,v] = find(X1a==255);
a= [r, c, v];
[r,c,v] = find(X2a==255);
b= [r, c, v];
c= [a; b];
Then I want to use ALL the coordinates that I have found (matrix c) to replace the elements in X1a AND X2a with the elements at the same coordinates from X1b and X2b respectively. However- I am unsure how to do this.
Any help would be most appreciated.

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 22 Apr 2014
Modificato: Andrei Bobrov il 22 Apr 2014
t1 = X1a == 255;
X1a(t1) = X1b(t1);
t2 = X2a == 255;
X2a(t2) = X2b(t2);
or
Xa = cat(3,X1a,X2a);
Xb = cat(3,X1b,X2b);
t = Xa == 255;
Xa(t) = Xb(t);
X1a = Xa(:,:,1);
X2a = Xa(:,:,2);

1 Commento

Thanks for your response
I was just wondering how I would edit this code so that it changed all the elements that equalled 255 in both matrices
As a simple example:
X1a= [ 123 123 123; 123 255 123; 255 123 123];
X1b= [ 4 4 4; 4 4 4; 4 4 4];
X2a= [255 111 111; 111 111 111; 111 111 111];
X2b= [5 5 5; 5 5 5; 5 5 5];
In X1a there are 2 elements that equal 255, and in X2a there is 1. I would like to change the 3 coordinates in both matrices so I ended up with:
X1c = [4 123 123; 123 4 123; 4 123 123]
X2c= [5 111 111; 111 5 111; 5 111 111]
Thanks again for your help

Accedi per commentare.

Richiesto:

il 22 Apr 2014

Commentato:

il 22 Apr 2014

Community Treasure Hunt

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

Start Hunting!

Translated by