Substituting particular matrix values

1 view (last 30 days)
Charlotte
Charlotte on 22 Apr 2014
Commented: Charlotte on 22 Apr 2014
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.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 22 Apr 2014
Edited: Andrei Bobrov on 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 Comment
Charlotte
Charlotte on 22 Apr 2014
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

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!