[英]how to generate a random matrix less than or equal to another matrix in python
I Want to generate a random 0-1 matrix less than or equal to another matrix in python.我想在 python 中生成一个小于或等于另一个矩阵的随机 0-1 矩阵。 for example, I have a specific matrix:
例如,我有一个特定的矩阵:
a=[1 0 0
0 1 1
0 0 1]
And I want to generate a random matrix:我想生成一个随机矩阵:
y=[1 0 0
0 0 0
0 0 1]
As we can see, a>=y.正如我们所见,a>=y。 How can I generate random matrix y in python?
如何在 python 中生成随机矩阵 y?
I'd say to generate a random matrix, then set down to 0 where the values are too high我会说生成一个随机矩阵,然后设置为 0 值太高
import numpy as np
a = np.array([[1, 0, 0], [0, 1, 1], [0, 0, 1]])
print(a)
y = np.random.randint(2, size=a.shape)
y[y > a] = 0
print(y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.