我需要构造一个知道行向量的2D矩阵。构造此矩阵的最简单方法是什么?
import numpy as np
a = [1,2,3]
A = np.zeros(3,3)
for i in range(0,3):
A[i][:]= a[i:3]
答案 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]