简体   繁体   English

如何在 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?

I have to generate a matrix with of shape 10^4 x 10^4 with 100 integer items randomly chosen 1 and 30 and the rest of the elements equals to 0. Can anybody help me?我必须生成一个形状为 10^4 x 10^4 的矩阵,其中 100 个 integer 项目随机选择 1 和 30,元素的 rest 等于 0。有人可以帮我吗? :pray :祈祷

Create the matrix with zeros, and then add the random numbers, or create an empty matrix, add the random numbers, and fill the empty values with 0.用零创建矩阵,然后添加随机数,或者创建一个空矩阵,添加随机数,然后用 0 填充空值。

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

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