Search code examples
matlabmatrixcomparisonmax

How to compare 2 matrices and keep greatest values in Matlab


I am using Matlab and have 2 matrices:

A =

 0     0     1
 1     0     1
 1     1     0

and

B =

 0     0     1.1
 1.0   0     0.8
 1.2   0.8   0

My goal is to compare the 2 matrices and get the highest values of each i,j element and store such results into a third matrix C. I am able to achieve such result by applying a for loop that checks every single element and then stores it into the matrix but i would like to have a more efficient and elegant way to do so. Can you suggest me some better way?

The result should be like:

C = 

 0     0   1.1
 1     0     1
 1.2   1     0

Solution

  • C = max(A,B)
    

    Source on documentation