简体   繁体   English

使用 np.tile 生成 (n, 1, 2) 数组

[英]Generate (n, 1, 2) arrays with np.tile

I want to create n times (1,2) arrays and each array should have the same elements.我想创建 n 次 (1,2) 数组,并且每个数组都应该具有相同的元素。 First I generate n times 1 D array and then I use a loop to iterate over these elements and repeat each element to fill (n, 1,2) array.首先,我生成 n 次 1 D 数组,然后使用循环遍历这些元素并重复每个元素以填充 (n, 1,2) 数组。 my code is the following:我的代码如下:

import numpy as np


def u_vec():
   return np.array([np.random.rand(1)])

n=10
u1 = np.zeros(n)

for i in range(n):
    u1[i] = u_vec()

print(u1)

def u_vec1():
    u_vec = np.zeros((n, 2,1))
    for i in range(len(u1)):
        u_vec[i] += np.tile(u1[i], (2,1))
    return u_vec

u = u_vec1()
print(u)

the output that I get is我得到的输出是

[0.4594466  0.80924903 0.3186138  0.03601917 0.9116031  0.68199505
 0.78999837 0.33778259 0.97626521 0.84925156]


[[[0.4594466 0.4594466]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]

[[0.        0.       ]]]

I do not understand why only the first element is filled but the others are filled with zero.我不明白为什么只有第一个元素被填充,而其他元素被零填充。 Could someone please help me?有人可以帮我吗? Thank you very much!非常感谢! the output that I would like to have我想要的输出

[[[0.4594466 0.4594466]]

[[0.3186138  0.3186138]]

[[ 0.03601917 0.03601917]]

[[ 0.9116031  0.9116031 ]]

[[0.68199505  0.68199505]]

[[0.78999837  0.78999837]]

[[0.33778259  0.33778259]]

[[0.97626521   0.97626521]]

[[0.84925156   0.84925156]]]]

I see the problem.我看到了问题。 The problem is that your return u_vec statement is enclosed in the for loop.问题是您的return u_vec语句包含在for循环中。 So only the first subarray is updated with the random values and the rest of u_vec remains 0 because you return immediately after the first iteration of the for loop.所以只有第一个子u_vec用随机值更新,其余的u_vec保持为 0,因为在 for 循环的第一次迭代后立即返回。 You should use你应该使用

def u_vec1():
    u_vec = np.zeros((n, 2,1))
    for i in range(len(u1)):
        u_vec[i] += np.tile(u1[i], (2,1))
    return u_vec # <---- moved outside the for loop

Having solved this problem, you might also be interested in knowing an alternative solution using repeat and reshape to get the desired result as解决了这个问题后,您可能还想知道使用repeatreshape的替代解决方案以获得所需的结果

import numpy as np

n=10
u1 = np.random.rand(n)
print(u1)

u = np.repeat(u1,2).reshape((n,2,1))
print(u)

[0.17106854 0.7346424  0.53370937 0.39838919 0.42247593 0.61545304
 0.97014742 0.85912941 0.51137618 0.08148184]
[[[0.17106854]
  [0.17106854]]

 [[0.7346424 ]
  [0.7346424 ]]

 [[0.53370937]
  [0.53370937]]

 [[0.39838919]
  [0.39838919]]

 [[0.42247593]
  [0.42247593]]

 [[0.61545304]
  [0.61545304]]

 [[0.97014742]
  [0.97014742]]

 [[0.85912941]
  [0.85912941]]

 [[0.51137618]
  [0.51137618]]

 [[0.08148184]
  [0.08148184]]]

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

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