[SciPy-user] retrieve indexes of 2D array element
David Warde-Farley
dwf at cs.toronto.edu
Mon Jul 13 07:26:35 EDT 2009
It doesn't require 3 inputs. Two of them are optional. See the
examples, or this:
In [20]: A = array([[1,4,9],[3,5,6],[9,9,3]])
In [21]: rows, cols = where(A == 9)
In [22]: rows
Out[22]: array([0, 2, 2])
In [23]: cols
Out[23]: array([2, 0, 1])
A == 9 will produce a boolean array equal to True where the element is
9 and False otherwise. Use this as the first argument to where.
David
On 13-Jul-09, at 4:03 AM, Robert Robert wrote:
> Hi,
> I've seen this function where, but it requires three inputs and I
> have no clue how to use it. Is it possible to give me an example.
> E.g I have an array as
>
> a = [ [1,2,3],
> [4,5,6],
> [7,8,9] ]
>
> and I would like to retrieve the indexes of values 9 which are 2, 2.
> How do I do that with scipy.where ?
> Thanks,
> robert
>
> --- On Mon, 7/13/09, David Warde-Farley <dwf at cs.toronto.edu> wrote:
>
> From: David Warde-Farley <dwf at cs.toronto.edu>
> Subject: Re: [SciPy-user] retrieve indexes of 2D array element
> To: "SciPy Users List" <scipy-user at scipy.org>
> Date: Monday, July 13, 2009, 7:11 AM
>
> On 13-Jul-09, at 2:56 AM, Robert Robert wrote:
>
> > Hi all,
> > I have a 2D scipy array of integer. I can not figure out how to
> > retrieve the indexes i, j of a specific element in the array. I can
> > loop through the array in python and search for it, but I can not
> > believe that scipy does not have a highly optimized algorithm.
> > Thanks,
> > robert
>
>
> By a specific element, do you mean a certain value? If so use a
> boolean condition and where().
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html
>
> David
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
More information about the SciPy-User
mailing list