简体   繁体   English

Python、Numpy:如何一次更改两个向量

[英]Python, Numpy: How to change two vectors at once

I'm trying to change two vectors of a NumPy matrix at once, but I'm losing one vector components:我正在尝试一次更改 NumPy 矩阵的两个向量,但我丢失了一个向量分量:

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.但我想保持第一列中的4s。 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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在NumPy中一次乘以3个以上的向量 - How can I multiply more than 3 vectors at once in NumPy 通过在Python中使用简单的numpy或数学在两个向量之间形成角度? - angle between two vectors by using simple numpy or math in Python? 使用 Numpy 对两个向量进行分组 - Grouping two vectors using Numpy 如何形成 numpy 中两个向量 arrays 之和的矩阵? - How can I form the matrix of sums of two arrays of vectors in numpy? 如何检查向量的元素是否位于 Numpy 中的两个向量之间? - How to check if elements of a vector lies between two vectors in Numpy? Numpy:如何逐个乘以两个向量,shape(n,1)和(n,)? - Numpy: How to elementwise-multiply two vectors, shape (n,1) and (n,)? Python / Numpy它叫什么/如何表示你将两个向量的每个元素相乘的操作? - Python/Numpy What's it called / how do you represent that operation where you multiply each element of two vectors? 如何在 python 中一次更改图像的所有特定值(3D NumPy 数组)? - How can I change all specific values of a image (3D NumPy array) at once in python? Python - 如何找到两个向量之间的相关性? - Python - How to find a correlation between two vectors? Python - 如何重塑两个向量并将其转换为元组? - Python - how to reshape two vectors and transform it into a tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM