python - Numpy 不交换元素
问题描述
我正在尝试交换 NumPy 的二维数组中的两个索引。不幸的是,只有一个元素被交换。这是代码:
n = len(A)
perMatrix = np.zeros((n,n))
np.fill_diagonal(perMatrix, 1)
perMatrix = A
# swapping the row
print(perMatrix)
temp = perMatrix[switchIndex1]
print(temp)
# perMatrix[switchIndex1][0] = 14
perMatrix[switchIndex1], perMatrix[switchIndex2] = perMatrix[switchIndex2], perMatrix[switchIndex1]
print(perMatrix)
这是代码输出的内容:
解决方案
您可以添加(在创建 perMatrix 后的行上):
sigma = [switchIndex1, switchIndex2]
tau = [switchIndex2, switchIndex1]
perMatrix[sigma,:] = perMatrix[tau,:]
推荐阅读
- python - 重复索引给定次数
- ldap - LDAP 389ds 与用户权限 ACI 和 Apache DS 作斗争
- jupyter-notebook - 如何撤消 Jupyter Notebook 中的双击?
- elasticsearch - 如果 Elasticsearch 不能跨分区平衡分片,为什么 Elasticsearch 允许您在 .yml 文件中指定多个磁盘分区?
- c# - 在 WinForms c# 中覆盖两个视频
- c# - 无服务器上的 Autofac 生命周期范围
- android - 在通话期间识别语音输入
- hadoop - 数据节点自动关闭并出现错误“WARN datanode.DataNode:Exiting Datanode”
- javascript - 如何在回调之外声明变量,在回调中修改它,然后在外面使用修改后的值?
- javascript - 谷歌云功能'node-pre-gyp/lib/pre-binding'