简体   繁体   English

如何用一维数组中的值替换 3D numpy 数组的第一维?

[英]How to replace the first dimension of a 3D numpy array with values from a 1D array?

I've got a 3D and a 1D numpy array- A sized (3750, 17, 1000) and B sized (3750).我有一个 3D 和一个 1D numpy 数组 - A 大小(3750、17、1000)和 B 大小(3750)。 I want to replace the values in the 1st dimension of A with the values from array B, so that the resulting array C is still sized (3750, 17, 1000), but the values in the first dimension are different.我想用数组 B 中的值替换 A 的第一维中的值,以便结果数组 C 的大小仍然是 (3750, 17, 1000),但第一维中的值不同。

>>> A.shape
(3750, 17, 1000)

>>> B.shape (3750,)

>>> C.shape(3750, 17, 1000)

I've tried:我试过了:

>>> C = np.concatenate((A, np.broadcast_to(np.array(B)[:, None, None],A.shape)), axis = 0)

But the output is:但输出是:

>>> C.shape (7500, 17, 1000)

So basically if所以基本上如果

A =一 =


1 [x, y ... 1000]

  [x, y ... 1000]

  ...17

2 [x, y ... 1000]

  [x, y ... 1000]

  ...17

3 [x, y ... 1000]

  [x, y ... 1000]

  ...17
.
.
.
3750

and B =和 B =

22
43
11
.
.
n=3750

Then C should look like那么 C 应该看起来像

22 [x, y ... 1000]
   [x, y ... 1000]
    ...17

43 [x, y ... 1000]
   [x, y ... 1000]
    ...17

11 [x, y ... 1000]
   [x, y ... 1000]
    ...17
.
.
.
n=3750

Do you mean:你的意思是:

A[:,0,0] = B

Is it correct?这是正确的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM