【问题标题】:How can I properly append a matrix to the bottom of an existing matrix in Python?如何正确地将矩阵附加到 Python 中现有矩阵的底部?
【发布时间】:2020-04-07 13:44:21
【问题描述】:

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

我尝试执行以下操作,但当我在 Python 中执行此操作时,它显示形状为 (2, 1, 500, 500)。

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

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

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

【问题讨论】:

标签: python python-3.x matrix linear-algebra


【解决方案1】:

使用np.vstack 最终工作。感谢您的帮助!

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

【讨论】:

    【解决方案2】:

    你试过numpy concatenate

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

    【讨论】:

    • 这得到了我 TypeError: concatenate() got multiple values for argument 'axis' ``` def get_Alambda(n, lamb): B = get_lambdaI(n, lamb) AB = np.concatenate(A , B, 轴 = 0) 返回 AB print(get_Alambda(n = 500, lamb = 10e-06)) ```
    • 您能否提供更多有关矩阵类型的信息?最好也包括您的数据,如a minimal reproducible example
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多