[Tutor] index of elements in numpy array

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Sep 13 22:48:46 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120913/585b49d9/attachment.html>


More information about the Tutor mailing list