我有一个问题:我想在 Python 中创建一个类似 struct 的 Matlab。我需要创建的结构有两个字段:“val”和“sl”。它必须是 1x2 结构。“val”字段内部需要有两个 3x3 矩阵(例如 A 和 B),而“sl”字段内部需要有两个值(例如 137 和 159)。最终的结构应该是这样的:
val sl
3x3 137
3x3 159
在 Matlab 中,代码是:struct(1).val=A;struct(1).sl=137;struct(2).val=B;struct(2).sl=159
在 python 中我尝试过hval = fromarrays([[A, B], [137, 159]], names=['val', 'sl'])
,但它给了我这个错误:文件“/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/ core/records.py",第 608 行,在 fromarrays 中引发 ValueError("array-shape mismatch in array %d" % k)
ValueError: array-shape mismatch in array 1 有谁知道如何解决这个问题?