[Tutor] array

Paul Sidorsky paulsid@shaw.ca
Sun, 17 Mar 2002 20:58:19 -0700


Karshi wrote:

>  How do find the maximum number in an array: say A[i,j] ?
> I've got the following error when I tried to use "max":
> --------------------------------------------------------------------
> >>> M=array([[1,2,3,],[3,4,56],[4,6,8]], Float32)
> >>> M
> array([[  1.,   2.,   3.],
>        [  3.,   4.,  56.],
>        [  4.,   6.,   8.]],'f')
> >>> max(M)
> array([ 4.,  6.,  8.],'f')
> ---------------------------------------------------------------------
> which is not true.

Actually it is true.  Python does a memberwise comparison of sequence
types, and 4>3>1 so the last row is the biggest according to Python's
comparison logic.

Anyhow, try this:

>>> max([max(row) for row in M])
56.0

This pulls the maximum out of each row and then takes the maximum of
those maximums.  

I don't know if there's a faster solution using NumPy itself, but at
least this is something that seems to work.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/