Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy - 2d array indexing

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?

like image 624
armandino Avatar asked Mar 03 '11 07:03

armandino


People also ask

How are 2D NumPy arrays indexed?

Indexing a Two-dimensional Array To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value '10,' use the index '3' for the row and index '1' for the column.

How is a 2D array indexed?

Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type.

Can you index a NumPy array?

Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples.

Is NumPy indexing fast?

Indexing in NumPy is a reasonably fast operation.


1 Answers

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

like image 183
Jon Avatar answered Oct 05 '22 02:10

Jon