numpy. ndarray swaps the row and column method for the multidimensional array of matrix
- 2020-12-05 17:16:13
- OfStack
As shown below:
>> import numpy as np
>> P = np.eye(3)
>> P
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Swap lines 0 and 2:
>> P[[0, 2], :] = P[[2, 0], :]
# P[(0, 2), :] = P[(2, 0), :]
>> P
array([[ 0., 0., 1.],
[ 0., 1., 0.],
[ 1., 0., 0.]])
Then swap column 1 and column 3:
>> P[:, [0, 2]] = P[:, [2, 0]]
>> P
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Note the following is the wrong approach:
>> P[0, :], P[2, :] = P[2, :], P[0, :]
>> P
array([[ 0., 0., 1.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
# Not to be wordy, and the meaning of the representation is not exchange