5

According to docs numpy's default behaviour is to index arrays first by rows then by columns:

a = numpy.arange(6).reshape(3,2)

[[0 1]
 [2 3]
 [4 5]]

print a[0][1] # is 1

I want to index the array using the geometrically oriented-convention a[x][y], as in x-axis and y-axis. How can I change the indexing order without modifying the array's shape so that a[0][1] would return 2?

4
  • I borrowed the terminology from the docs (see the link): "Matrix notation uses the first index to indicate which row is being selected and the second index to indicate which column is selected. This is opposite the geometrically oriented-convention for images where people generally think the first index represents x position (i.e., column) and the second represents y position (i.e., row)" Commented Mar 3, 2011 at 7:58
  • Nevermind, was thinking something else. Commented Mar 3, 2011 at 8:00
  • 3
    Just as a note, when indexing multi-dimensional arrays you should use the notation a[i,j] rather than a[i][j]. I find on my machine that the later is 2x slower. Commented Mar 3, 2011 at 12:55
  • @Josh Thanks for the tip. It looks more readable too. Commented Mar 3, 2011 at 16:03

1 Answer 1

8

You can write a.T[0,1] to use indices of the transpose of the array, which are the other way around in 2D.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this create a new transposed array each time I access using this notation?
The .T only creates a view of the original array, so you are not copying data. It should be relatively fast.
Wouln't a.T[0,1] be better, because it would leave out the intermediate array avoiding to call the getitem routine twice?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.