Suppose there is an 2-dimensional numpy array (or matrix in maths) named "A", I want to swap its 1st row with another row "n". n is can be any natural number. The following codes do not work
A = np.eye(3)
n = 2
A[0], A[n] = A[n], A[0]
print(A)
you can see it gives (I show the matrix form A for simplicity)
A =
0, 0, 1
0, 1, 0
0, 0, 1
But what I want is
A =
0, 0, 1
0, 1, 0
1, 0, 0
I thought about one solution is introducing another matrix "B" which is equal to "A", but "A" and "B" are different objects. Then do this
A = np.eye(3)
B = np.eye(3)
A[0], A[n] = B[n], B[0]
This can give the correct swap on A. But it needs an additional matrix "B", I don't know if it is computational efficient. Or maybe you have better idea? Thanks :)
A[[0,n]] = A[[n,0]]