简体   繁体   English

Append 矩阵到 numpy 数组

[英]Append matrices to a numpy array

testMat1 = np.array([[1,2,3,4],[4,5,6,7]])
testMat2 = np.array([[7,8,9,10],[10,11,12,13]])
testMat3 = np.array([[2,4,6,8],[3,5,7,9]])

Here are three matrices of shape (2, 4)这是三个形状矩阵(2, 4)

How do I combine them into a multidimensional array with shape (3, 2, 4) ?如何将它们组合成形状为(3, 2, 4)的多维数组?

np.array([testMat1, testMat2, testMat3]) works properly, however this is not what I am looking for because I will be continuously adding more matrices to the array. np.array([testMat1, testMat2, testMat3])工作正常,但这不是我正在寻找的,因为我将不断向数组添加更多矩阵。 I need a way to append new matrices to the array.我需要一种方法将 append 个新矩阵添加到数组中。 I tried using np.append but it doesn't seem to be meant for this purpose.我试过使用np.append但它似乎并不是为了这个目的。

You can use np.vstack() to vertically stack the arrays.您可以使用np.vstack()垂直堆叠 arrays。

In your case the command would look like this: combined = np.vstack(([testMat1], [testMat2], [testMat3])) which will give you the shape (3, 2, 4)在你的情况下,命令看起来像这样: combined = np.vstack(([testMat1], [testMat2], [testMat3]))这会给你形状(3, 2, 4)

You can continuously add more arrays and update it by using: combined = np.vstack((combined, [testMat4])) which will give you the shape (4, 2, 4)您可以不断添加更多 arrays 并使用以下方法更新它: combined = np.vstack((combined, [testMat4]))这将为您提供形状(4, 2, 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM