Matrices: Matrices in Python
Selection of components and assignment of values
Selection of components and submatrices You can select components of arrays in various ways. We give a few examples showing that indexing starts at (0, 0) and that there are smart ways to create submatrices.
>>> import numpy as np
>>> M = np.array([range(2,6), range(6, 10), range(10, 14)]); print(M)
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
>>> M[1,2] # a single element, in the 2nd row and 3rd column
8
>>> M[:, 3] # third column
array([ 4, 8, 12])
>> M[0] # first row
array([2, 3, 4, 5])
>> M[0, 1:] # first row, all except first column
array([3, 4, 5])
>> M[0:2, 0:2] #
submatrixarray([[2, 3],
[6, 7]])
Selection via logical expressions
You can also use logical indexing (with zeros and ones) to select matrix elements. You need relational operators for this, such as the ones in the table below. Besides logical indexing you can also use the where
instruction to find suitable indices for a logical expression.
== | equal to | ~= | not equal to |
< | less than | > | greater than |
<= | less than or equal to | >= | greater than or equal to |
>>> M = np.array([range(2,6), range(6, 10), range(10, 14)]); print(M)
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]] >>> # value true if the condition is met, false otherwise
>>> lM = M>7; print(lM)
[[False False False False]
[False False True True]
[ True True True True]]
>>> M(lM) # rowwise selection of components
array([ 8, 9, 10, 11, 12, 13])
>>> ij = np.where(M<9); print(ij) % selection of indices via where command
(array([0, 0, 0, 0, 1, 1], dtype=int64), array([0, 1, 2, 3, 0, 1], dtype=int64))
>>> M[ij] # rowwise selection of components
array([2, 3, 4, 5, 6, 7])
Assignment Components to which you refer can also be assigned new values.
>>> import numpy as np
>>> M = np.array([range(2,6), range(6, 10), range(10, 14)]); print(M) [[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
>> M[2,3] = 17; print(M) # change of one matrix entry
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 17]]
>>> M[:,3] = 1; print(M) # assign all entries in fourth column the value 1 [[ 2 3 4 1]
[ 6 7 8 1]
[10 11 12 1]] >>> M = np.delete(M, 0, 0); print(M) # remove the first row [[ 6, 7, 8, 1]
[10, 11, 12, 1]] >>> M[:] = 0; print(M) # assign all matrix entries the value 0 [[0 0 0 0]
[0 0 0 0]]
By assignments you can modify the the structure provided that you are left with a matrix. In the example below we give a matrix extra entries by assignment. By the way, this is not a recommended way to work. Pre-reserving ample space for components in a matrix structure through the zeros construction, followed by filling the matrix with values through assignments, is a better way to collect data in Python experiments.
>>> M = np.array([[2,4], [3,5]]); print(M)
[[2 4]
[3 5]]
>>> v = np.array([[6],[7]]); print(v)
[[6]
[7]]
>>> M = np.append(M, v, axis=1); print(M)
[[2 4 6]
[3 5 7]]
Change of shape You can change the shape from a vector or matrix. The following two examples illustrate this.
>>> v = np.array(range(1,7)); print(v) # vector of length 6
[1 2 3 4 5 6]
>>> A = np.reshape(v, (3, 2)); print(A) # 3x2 matrix
[[1 2]
[3 4]
[5 6]]
>>> B = np.reshape(A, (2, 3)); print(B) # 2x3 matrix
[[1 2 3]
[4 5 6]]