Tony Tannous
Tony Tannous

Reputation: 473

Pick the values from the corresponding max matrix

I don't think I asked the question right, so this example will make it easier for you to understand what I mean.

Lets say I have 2 matrices

A = [5,5; 7,7];
B = [2,2; 6,4];

And another 2 matrices, each one correspond with one of the above. Lets say

A' = [7,7; 9,9];
B' = [1,1; 10,5];

And I need to construct a new Matrix, that will check each pixel in A' and B', pick the max, then goes to the corresponding Matrix and extract the value from there.

In this example I will get this newMat

newMat = [5,5; 6,7];

It is easy to be done with loops, is there a way to do it with out using loops ? Thanks in advance!

Upvotes: 2

Views: 39

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

Here's another approach. Let your inputs be

A = [5,5; 7,7];
B = [2,2; 6,4];
Aprime = [7,7; 9,9];
Bprime = [1,1; 10,5];

Then

newMAt = (Aprime>Bprime).*A + (Aprime<=Bprime).*B;

Upvotes: 2

Suever
Suever

Reputation: 65430

You can create a logical matrix of where Aprime is more than Bprime and that can then be used to grab values from either A or B

aprime_is_greater = Aprime > Bprime;

% Initialize C to B and replace values where Aprime was greater
C = B;
C(aprime_is_greater) = A(aprime_is_greater);

Upvotes: 3

Related Questions