简体   繁体   中英

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)

How do I combine them into a multidimensional array with shape (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. I need a way to append new matrices to the array. I tried using np.append but it doesn't seem to be meant for this purpose.

You can use np.vstack() to vertically stack the 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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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