【问题标题】:How do I multiply a 1D array with only one other array in a 3D array?如何将 1D 数组与 3D 数组中的一个其他数组相乘?
【发布时间】:2020-04-16 03:25:22
【问题描述】:

我有一个 1D 数组,我想将它与 3D 数组的第一个轴相乘。

例如,我的 1D 数组的长度为 710,我想将它与长度也为 710 的 3D 数组的第一个轴相乘。我不想要它与其他两个轴相乘(因为它们的大小不同,我得到一个错误)。

我该怎么做?

下面是示例代码:

    data = sla_standard[:,:,:]
    window = w

    print(window.shape)
    print(data.shape)

    #GOAL: multiply the window with the first axis of my data array

    what is printed from console: 
    (710,)
    (710, 81, 320)

【问题讨论】:

    标签: python arrays function multidimensional-array


    【解决方案1】:

    两个数组应该具有相同的维度(即应该是 3D)。一旦它是真的,numpy 将broadcast 自动丢失维度(即,只有一行/列的维度将被拉伸以匹配另一个数组)。使用window[:, None, None] 将其转换为 3D:

    >>> window = np.random.random((710))
    >>> data = np.random.random((710, 81, 320))
    >>> (window[:, None, None] * data).shape
    (710, 81, 320)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多