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]]