user3541731
user3541731

Reputation: 1

Specifically filling new matrix with elements from two different matrices in matlab

I am fairly new to matlab and am struggling to write code to fill a matrix

I have two matrices I1 and I2 (both have dimensions 255x255) I would like to write code to create a new matrix that has the element values from I1 in, unless the element equals 255- in which case I would like to use the element from the same coordinate from I2 instead.

I hope this makes sense! Thanks for your help :)

Upvotes: 0

Views: 44

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

Using logical indexing:

result = I1;
ind = result==255; %// logical index
result(ind) = I2(ind);

Or using a mask:

ind = I1==255; %// logical index, used as a mask
result = ~ind.*I1 + ind.*I2;

Upvotes: 1

Related Questions