简体   繁体   中英

How can I properly append a matrix to the bottom of an existing matrix in Python?

I have two matrices, A and B. A and B are each 100 x 100. I am trying to produce a combined matrix AB such that is 200 x 100 with the elements of A in the first 100 x 100 and the elements of B in the second 100 x 100.

I tried doing the following, but it says the shape is (2, 1, 500, 500) when I do this in Python.

def get_bigAB(n, lamb):
    return np.array([[A], [get_B(n, lamb)]])

My entries are floats, not simple integers.

My get_B function performs as expected, and of course, I am using Python 3.

Using np.vstack ended up working. Thanks for helping!

def get_Alambda(n, lamb):
    B = get_lambdaI(n, lamb)
    AB = np.vstack((A, B))
    return AB

Have you tried numpy concatenate

import numpy as np
AB = np.concatenate((A,B),axis=0)
print(AB)
print(AB.shape)

Try using vstack(A, B) where matrix B is appended to the bottom of matrix A. This will give you the dimensions you want.

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