[Tutor] index of elements in numpy array
akleider at sonic.net
akleider at sonic.net
Fri Sep 14 00:32:56 CEST 2012
> On 13 September 2012 21:16, Bala subramanian
> <bala.biophysics at gmail.com>wrote:
>
>> Friends,
>> I have a 2D matrix (a numpy array) and i am looping over each row in
>> the matrix. I would like to know how i can find the index of each
>> element in a while looping a row. Is there any function in numpy.
>>
>
> Your question could mean several things. Could you post a small piece of
> example code and clarify what it is you're unable to do?
>
> Here's an example that loops over all elements of a 2D Matrix:
>
>>>> import numpy
>>>> M = numpy.array([[1, 1], [2, 3], [5, 8]])
>>>> M
> array([[1, 1],
> [2, 3],
> [5, 8]])
>>>> numrows, numcols = M.shape
>>>> for i in range(numrows):
> ... for j in range(numcols):
> ... print M[i, j]
> ...
> 1
> 1
> 2
> 3
> 5
> 8
>
> Oscar
I think what he wants is to replace your print statement with
"""
if <some condition>:
print "Indeces are %d and %d."%(i, j, )
"""
More information about the Tutor
mailing list