2.16 Find the location of the maximum value in a matrix

Mathematica

mat ={{2,4,-3}, 
      {7,9,5}, 
      {0,5.4,9}} 
 
m = Max[mat];  (*this gives values *) 
Position[mat,m](*this find locations*)
 

Out[142]= {{2,2},{3,3}}
 

 

Matlab

h = [2, 4, -3; 
     7, 9, 5; 
     0, 5.4, 9] 
 
m = max(h(:)); 
 
[r,c]=find(h==m)
 

r = 
     2 
     3 
c = 
     2 
     3
 

 

Maple

This below finds position of first max.

A:=Matrix([[2,4,-3],[7,9,5],[0,5.4,9]]); 
v:=max(A); 
member(v,A,'pos'); 
pos
 

     2,2
 

 

Maple support for such operations seems to be not as strong as Matlab. One way to find locations of all elements is by using explicit loop

A:=Matrix([[2,4,-3],[7,9,5],[0,5.4,9]]); 
v:=max(A); 
r:=[seq(seq(`if`(A[i,j]=v,[i,j],NULL),i=1..3),j=1..3)];
 

    [[2, 2], [3, 3]]