【问题标题】:Numpy - How generate the following n-dimensional matrixNumpy - 如何生成以下 n 维矩阵
【发布时间】:2021-08-23 09:58:28
【问题描述】:

让我们考虑两个矩阵“A,B”$(2 \times 2)$,我怎样才能得到下面的结果“C”,使用numpy?

这里是矩阵 A 和 B 的代码:

import numpy as np

A = np.array([
    [1, 1],
    [2, 2],
])

B = np.array([
    [0.5, 0.3],
    [0.1, 0.6],
])

# My desired output: 
C = [
    [
        [1*0.5, 1*0.3],
        [1*0.1, 1*0.6],
    ],
    [
        [2*0.5, 2*0.3],
        [2*0.1, 2*0.6],
    ],
]

提前致谢!!

【问题讨论】:

  • 你能把矩阵写成代码块,每一行在不同的行吗?我认为 LaTex 格式适用于数学堆栈交换;但我认为他们不适用于 SO。
  • 感谢您的建议,我去调整问题!
  • 到目前为止你尝试过什么?如果我们为你做功课,我们会得到功劳吗?
  • np.outer 可能有用,.reshape 可能有用。 np.einsum 几乎可以带你到任何地方,只要你设法绕开它。

标签: python arrays numpy numpy-ndarray


【解决方案1】:

您可以add a new axis 到您的第一个数组,以便乘以该轴:

C = A[:, np.newaxis]*B

【讨论】:

    猜你喜欢
    • 2020-03-20
    • 2021-06-04
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多