Python 中的 N 维矩阵数组(不同大小)
在 Matlab 中,有一种叫做 struct 的东西,它允许用户拥有一组动态的矩阵。
我基本上正在寻找一个函数,该函数允许我对具有不同大小的动态矩阵进行索引。
示例:(有 3 个矩阵)
- 矩阵 1: 3x2
- 矩阵 2: 2x2
- 矩阵 3: 2x1
基本上我想将 3 个矩阵存储在同一个变量上。之后通过索引号来调用它们(即Matrix[1]、Matrx[2])。传统的Python数组不允许堆叠不同维度的数组。
我正在考虑创建课程,但也许有人有更好的选择。
谢谢
In Matlab, there is something called struct, which allow the user to have a dynamic set of matrices.
I'm basically looking for a function that allows me to index over dynamic matrices that have different sizes.
Example: (with 3 matrices)
- Matrix 1: 3x2
- Matrix 2: 2x2
- Matrix 3: 2x1
Basically I want to store the 3 matrices on the same variable. To called them by their index number afterward (i.e. Matrix[1], Matrx[2]). Conventional python arrays do not allow for arrays with different dimensions to be stacked.
I was looking into creating classes, but maybe someone her has a better alternative to this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用元组或列表。
元组
matrices = tuple(matrix1,matrix2,matrix3)
会稍微更高效;列表matrices = [matrix1,matrix2,matrix3]更加灵活,因为您可以
matrix.append(matrix4)
。无论哪种方式,您都可以通过
matrices[0]
或for matrix in matricies: pass # do stuff
来访问它们。Just use a tuple or list.
A tuple
matrices = tuple(matrix1, matrix2, matrix3)
will be slightly more efficient;A list
matrices = [matrix1, matrix2, matrix3]
is more flexible as you canmatrix.append(matrix4)
.Either way you can access them as
matrices[0]
orfor matrix in matricies: pass # do stuff
.将这些数组放入列表中。
Put those arrays into a list.