简体   繁体   English

如何在python中生成一个小于或等于另一个矩阵的随机矩阵

[英]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.

相关问题 如何在Python中生成随机协方差矩阵? - How to generate a random covariance matrix in Python? 如何在python中生成随机稀疏Hermitian矩阵? - How to generate a Random Sparse Hermitian Matrix in python? 如何在 Python 中生成一个矩阵,其中 n 个随机元素介于 1 和 30 之间,并且元素的 rest 等于零? - How can I generate in Python a matrix with n random elements between 1 and 30 and the rest of the elements equal to zeros? python - 如何使用随机打印总和小于或等于另一个整数的整数? - How to use random to print integers whose sum is less than or equal to another integer in Python? 我可以在Python中生成每行等于1的随机矩阵值(十进制)吗? - Can I generate random matrix value (decimals) in Python which the equal of each rows equal to 1? Python:如何使用Python生成随机稀疏对称矩阵? - Python: how to use Python to generate a random sparse symmetric matrix? 如何使用 Python 生成随机 N 维旋转矩阵? - How to generate a random N dimensional rotation matrix using Python? 如何在python中生成无重复的随机整数矩阵 - how to generate a matrix of random integers without duplicates in python Python:如何生成一个 1.0 和 0.0 的随机矩阵,但带有浮点数? - Python: How do I generate a random matrix of 1.0 and 0.0, but with floats? Python循环生成-1或1的随机二进制矩阵 - Python Loop to generate random binary matrix of -1 or 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM