gpt4 book ai didi

python - 打印矩阵中值的位置 MATRIX

转载 作者:太空宇宙 更新时间:2023-11-03 21:46:22 25 4
gpt4 key购买 nike

我有一个包含一些值的矩阵。我想将每个值的 m,n 个位置放入一个单独的矩阵中。

import numpy as np

a = np.zeros((3,3)) #Creating "a" matrix with all zeros
vals = [22,34,43,56,37,45] # the values that i want to add to the matrix "a"
pos = [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)] # Corresponding positions of the above given values

rows, cols = zip(*pos) # This code is to assign the given values in the correct position

a[rows, cols] = vals

print (a)

输出=

[[(0,0) (0,1) (0,2)]

[(1,0) (1,1) (1,2)]

[(2,0) (2,1) (2,2)]]

请帮帮我。

我想获得任何矩阵(m,n)的位置矩阵(m,n)

最佳答案

根据我的理解,使用下面的应用程序代码(在 python 3.6.2 中测试):

import numpy as np
matrixlist=[]
tuple1=()
list1=[]
list1=tuple(tuple1)
rows= 3
cols=3

a = np.zeros((rows*cols))
for i in range(rows):
list1=[]
list1.append(i)
t1=tuple(list1)
for j in range(cols):
list1=list(t1)
list1.append(j)
t2=tuple(list1)
matrixlist.append(t2)
list1.remove(list1[0])
data=[]
data= matrixlist
i=0
m=[data[i:i+cols] for i in range(0, len(data), cols)]
print(m)

输出:

[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]]

希望这能完全满足您的要求!

关于python - 打印矩阵中值的位置 MATRIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com