Skip to main content

12.10) 2D Array

Order of dimensions can be a bit confusing, and varies from one programming language to another. By default, NumPy uses the C convention that the fastest varying dimension is the last on. This concept can be a bit difficult to get your head around. All you need to remember is that a 2D array is represented in (row, column). It’s also possible to change the behaviour or Numpy to follow the Fortran convention rather than the C convention. If you want to know more check the row-major definition in the NumPy glossary (http://docs.scipy.org/doc/numpy/glossary.html).

Indexing in multi-dimension is best done in the following way A[row,column]:

>>> A=np.array([[1,2,3],[4,5,6]])
>>> A
array([[1, 2, 3],
       [4, 5, 6]])
>>> A[0,1]
2

When you want to select all elements over a dimension, use a colon:

>>> A[0,:]  # select first row
>>> A[:,1]  # second column

Notice that selecting a row or column returns an array, but in both cases, the arrays are one dimensional vectors. There is no distinction between a row and column vector.