I'm trying to change two vectors of a NumPy matrix at once, but I'm losing one vector components:
import numpy as np
data = np.array([[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]])
last = data[:, -1]
print(last)
data[:, 1:] = data[:, :-1]
data[:, 0] = last
print(data)
Gives this result:
[4 4 4 4]
[[3 1 2 3]
[3 1 2 3]
[3 1 2 3]
[3 1 2 3]]
But I want to maintain the 4s in the first column. Is there any form to accomplish that?
Use:
>>> np.roll(data, 1, 1)
array([[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.