如何正确地将矩阵附加到 Python 中现有矩阵的底部?

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

我有两个矩阵,A 和 B。A 和 B 都是 100 x 100。我正在尝试生成一个组合矩阵 AB,它是 200 x 100,A 的元素在前 100 x 100 和第二个 100 x 100.

中 B 的元素

我尝试执行以下操作,但在 Python.

中执行此操作时显示形状为 (2, 1, 500, 500)
def get_bigAB(n, lamb):
    return np.array([[A], [get_B(n, lamb)]])

我的条目是浮点数,而不是简单的整数。

我的 get_B 函数按预期执行,当然,我使用的是 Python 3.

你试过了吗numpy concatenate

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

使用 np.vstack 结束了工作。感谢您的帮助!

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

尝试使用 vstack(A, B),其中矩阵 B 附加到矩阵 A 的底部。这将为您提供所需的维度。