Kwstas Tranos
Kwstas Tranos

Reputation: 11

Matlab how to compare matrices and take max

Hi i have this equation in mathematics:

 φ= max(a,-r)

where a,r are matrices with the same dimensions.

How can i take the result of this in Matlab.

Thank you in advance.

Upvotes: 1

Views: 66

Answers (2)

Divakar
Divakar

Reputation: 221504

Approach #1: Using logical indexing -

φ = a.*(a>(-r)) + (-r).*~(a>(-r))

Approach #2: Creating an array of dimension one grater than a and r and using max along the highest dimension of the resultant array -

φ = max(cat(ndims(a)+1,a,-r),[],ndims(a)+1)

Using approach #2 would make more sense when you have a series of inputs among which the max is to be found out, i.e. for a case like max(a,r1,r2,r3..).

Upvotes: 1

Rash
Rash

Reputation: 4336

I think just

phi = max(a,-r);

would do.

Upvotes: 2

Related Questions