使用行向量构造2D矩阵的最简单方法是什么?

时间:2019-05-18 22:55:33

标签: python matrix 2d row

我需要构造一个知道行向量的2D矩阵。构造此矩阵的最简单方法是什么?

Matrix A

import numpy as np

a = [1,2,3]
A = np.zeros(3,3)
for i in range(0,3):
    A[i][:]= a[i:3]

1 个答案:

答案 0 :(得分:0)

使用a[i+1:i+2] + b[:-1]向右移动所有元素(在b中,并在开始时从原始a添加新元素

a = [0,1,2,3]

A = []

b = a[:] # first row without changes
for i in range(len(a)):
    print(b)
    A.append(b)
    b = a[i+1:i+2] + b[:-1] # in next row move right and add new element at the beginning 

print(A)

结果:

[0, 1, 2, 3]
[1, 0, 1, 2]
[2, 1, 0, 1]
[3, 2, 1, 0]