Slices are similar to those of lists (see also Section 3.1.1: Slicing) except that they might now be in more than one dimension:
- M[i,:] is a vector filled by the row of .
- M[:,j] is a vector filled by the column of .
- M[2:4,:] is a slice of 2:4 on the rows only.
- M[2:4,1:4] is a slice of rows and columns.
The result of matrix slicing is given in the following Figure 4.1:
Figure 4.1: The result of matrix slicing
If you omit an index or a slice, NumPy assumes you are taking rows only. M[3] is a vector that is a view on the third row of M and M[1:3] is a matrix that is a view on the second and third rows of .
Changing the elements of a slice affects the entire array (see also Section 5.1: Array views and copies):
v = array([1., 2., 3.]) v1 = v[:2] # v1 is array([1., 2.]) v1[0] = 0. # if v1 is changed ... v # ... v is changed too: array([0., 2., 3.])
General slicing rules...