You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Octave中创建每行递加10、列递加1的矩阵?

实现指定规则的矩阵

方法1:纯Python列表推导(无需额外库)

直接通过嵌套列表生成符合要求的矩阵,核心是用行索引控制列间的+1增量,列索引控制行间的10倍增量:

# 定义矩阵的行数、列数和起始值
rows = 3
cols = 3
start_num = 1

# 生成矩阵
matrix = [[start_num + i + j * 10 for j in range(cols)] for i in range(rows)]

# 按示例格式打印结果
for row in matrix:
    print(" ".join(map(str, row)))

运行后输出:

1 11 21
2 12 22
3 13 23

调整rows和cols的值就能生成任意大小的同规则矩阵,比如改成rows=4, cols=5可得到4行5列的对应矩阵。

方法2:使用NumPy库(适合数值计算场景)

如果学过NumPy,利用其广播特性可以更简洁实现:

import numpy as np

rows = 3
cols = 3
start_num = 1

# 生成每行的基础增量序列:[0,10,20]
row_increments = np.arange(0, cols * 10, 10)
# 生成每列的基础增量序列:[0,1,2],并转为列向量
col_increments = np.arange(0, rows).reshape(-1, 1)

# 广播相加得到最终矩阵
matrix = col_increments + row_increments + start_num
print(matrix)

运行后输出:

[[ 1 11 21]
 [ 2 12 22]
 [ 3 13 23]]

内容的提问来源于stack exchange,提问作者Lwelly Ebah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:00:01