[Tutor] Finding the minimum value in a multidimensional array
Geoframer
geoframer at gmail.com
Tue Mar 13 11:57:14 CET 2007
Hey everyone,
I've been trying to locate a way to find the location of the minimum value
in an n*n array.
For instance suppose we have a 10x10 array.
In [1]: import numpy
In [2]: a = numpy.zeros((10,10))
In [3]: a
Out[3]:
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
And we set a value in that array to be the smallest just for illustration
purposes.
In [4]: a[9][8] = -1
In [5]: a
Out[5]:
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., -1., 0.]])
I can find this minimum using the 'min' function, but is there a way to find
the index to that position?
In [6]: a.min()
Out[6]: -1.0
Any help is appreciated. Please keep in mind that this is only a 'small'
example. What i'm trying to accomplish is to find the location of the
minimum value in a huge n*n array and quickly and efficiently finding the
index to that position.
Regards - Geofram
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070313/0a6b8341/attachment.htm
More information about the Tutor
mailing list