Skip to content

矩阵转置

难度:⭐⭐

1. 题目描述

题目描述

  • 输出给定矩阵的转置结果
  • 已知矩阵为:[[1, 2, 3], [4, 5, 6]]

2. 关键点

  • for 循环

3. 代码实现

点击显示代码
python
jz1 = [
    [1, 2, 3],
    [4, 5, 6],
]
jz1_row_len = len(jz1)
jz1_col_len = len(jz1[0])

jz2 = []
jz2_row_len = jz1_col_len
jz2_col_len = jz1_row_len

for i in range(jz2_row_len):
    row = []
    for j in range(jz1_row_len):
        row.append(jz1[j][i])
    jz2.append(row)

print(f'源矩阵为:{jz1}')
print(f'转置矩阵为:{jz2}')

4. 运行示例

txt
源矩阵为:[[1, 2, 3], [4, 5, 6]]
转置矩阵为:[[1, 4], [2, 5], [3, 6]]

5. 进阶思考

  1. 可输入任意矩阵