numpy:打印具有随机元素、列和行的矩阵 [英] numpy: Print matrix with random elements, columns and rows

查看:47
本文介绍了numpy:打印具有随机元素、列和行的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用随机列(0, 9)和随机行(0, 9)和随机元素(0, 9)打印一个矩阵
其中 (0, 9) 是 0 到 9 之间的任意随机数.

I want a matrix to be printed with random columns(0, 9) and random rows(0, 9) with random elements(0, 9)
Where (0, 9) is any random number between 0 and 9.

推荐答案

如果您要查找的是一个 10x10 矩阵,其中填充了 0 到 9 之间的随机数,这就是您想要的:

If what you're looking for is a 10x10 matrix filled with random numbers between 0 and 9, here's what you want:

# this randomizes the size of the matrix.
rows, cols = np.random.randint(9, size=(2))
# this prints a matrix filled with random numbers, with the given size.
print(np.random.randint(9, size=(rows, cols)))

输出:

[[1 7 1 4 4 4 4 3]
 [1 4 7 3 0 5 3 5]
 [6 3 3 7 5 7 6 1]
 [3 8 5 7 2 0 1 6]
 [5 0 8 5 0 1 5 1]
 [1 3 3 7 3 7 5 6]
 [3 7 4 1 8 3 7 8]
 [8 8 8 5 8 4 7 1]]

这篇关于numpy:打印具有随机元素、列和行的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆