以下是Python代码示例:

import random

# 定义矩阵的行列数
rows = 3
cols = 4

# 创建一个空的矩阵
matrix = []

# 使用循环随机赋值
for i in range(rows):
    row = []
    for j in range(cols):
        row.append(random.randint(1, 10))
    matrix.append(row)

# 输出矩阵
for row in matrix:
    print(row)

输出结果类似如下:

[2, 5, 9, 5]
[9, 2, 7, 7]
[8, 5, 6, 9]
创建一个整型数组矩阵并随机赋值

原文地址: https://www.cveoy.top/t/topic/eF4Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录