Extracting part of a vector or matrix



>>> v = np.array([1,-1,1])
>>> print(v[0:2])
[1, -1]
>>> print(v[:])
[ 1 -1  1]
>>> print(v[0:3:2])
[1 1]
>>> C = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> print(C)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
>>> print(C[:,0])
[1 4 7]

To extract a range of values, use the subscript range [j:k]. You get the values from j, j+1, k-1. Note that it stops just before k. A colon : by itself specifies all values. Of course, this is not needed for a vector. You can skip values in the range. If you do j:k:m}, then you get j, j+m, j+2m, etc. up to the last value before |verb|k|.

A two-dimensional matrix takes two subscripts. So in the example above, we the subscript :,0 selects the whole 0th column of the matrix.