生成每行末尾为零的随机3x4矩阵

问题描述 投票:0回答:1

如何生成每行末尾带有3x4且为零的随机numpy矩阵?

例如:

[
 [[2.607265   7.7574973  4.662305   0.        ]
  [9.273584   6.389093   0.08433626 0.        ]
  [0.51892513 6.944563   9.47866    0.        ]]

 [[3.2981935  4.6587496  4.151615   0.        ]
  [0.28066677 5.505256   8.469639   0.        ]
  [9.009897   4.014221   6.632978   0.        ]]
...
]

我当前正在使用:

 def get_rand_m34(count, mn, mx):
    result = np.random.uniform(mn, mx, (count, 3, 4)).astype("float32")
    for m in result:
        m[0,3] = 0.0
        m[1,3] = 0.0
        m[2,3] = 0.0
    return result

是否可以仅使用numpy而没有for循环?

python-3.x numpy
1个回答
0
投票

使用numpy的高效切片

result[:,:,3] = 0.0

这将获取结果中每个2d数组中每一行的最后一列并将其设置为零。

© www.soinside.com 2019 - 2024. All rights reserved.