这是问题所在:
我想配对3个状态(3D向量),例如((a,a),(a,b),(a,c),(b,a),(b,b)、。 ..)转换为3X3矩阵的元素。
以更抽象的方式,我有一组3个数组和3个条目,并且我想生成一个3x3数组,其中条目本身是2个原始数组的数组。(很多嵌套)。
对于3个笛卡尔基向量,它看起来像:a =(1,0,0),b =(0,1,0),c =(0,0,1)
矩阵为:
M = | {(1,0,0),(1,0,0)} {(1,0,0),(0,1,0)} {(1,0,0),(0,0,1)} |
| {(0,1,0),(1,0,0)} {(0,1,0),(0,1,0)} {(0,1,0),(0,0,1)} |
| {(0,0,1),(1,0,0)} {(0,0,1),(0,1,0)} {(0,0,1),(0,0,1)} |
# I appreciate I have formatted the matrix in code rather than LaTeX
# but I can't find the functionality to do this on StackOverflow
因此矩阵中的元素是2个条目的数组,这些数组的条目是我的3D向量(3个条目的数组)。
我用来实现此目的的代码是:
import numpy as np
a= np.array([0,0,0])
b= np.array([0,0,1])
c= np.array([0,2,0])
states = np.array([a,b,c])
def cycler(states):
matrix = np.zeros((3,3))
for x in range(0,3):
for y in range(0,3):
matrix[x,y]= np.array([states[x],states[y] ])
return matrix
cycler(states)
但是,当我运行此代码时,在行上出现错误:
matrix[x,y]= np.array([states[x],states[y] ])
错误消息:
ValueError:设置具有序列的数组元素。
作为python的新手,我真的很感谢能帮助我创建矢量对矩阵的帮助!(就上下文而言,我的状态向量a,b,c不是基础向量,然后我将在以后的计算中使用2个3D状态的数组编写一个将读取矩阵元素的函数。)
设置matrix = np.zeros((3,3),dtype=object)
可以解决您的问题,尽管我建议您宁愿使用尺寸为3x3x2x3的4d数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句