我有一个列表[1,2,3,4,5...,68]
我希望它为[[1,1,1....68 times],[2,2,2...68 times],.....[68,68,68... 68 times]]
。
即如果我将列表视为矩阵,那么我有68x1矩阵想通过重复列中的元素使其成为68x68,就像MATLAB中的repmat function一样。
我试过像这样的np.tile
np.tile(y_1,(1,68))
其中y_1有68个元素,但最终给出了[1,2,3,4 ...... 68]
我也试过
y_1*68
但它给了[1,2,3,4....68,1,2,3,4.....68,1,2...68....]
这也不是我想要的
我该怎么做?
答案 0 :(得分:2)
您可以使用list comprehension
。
lis = [1,2,3,4,5]
rep = [[i]*len(lis) for i in lis]
print(rep)
输出:
[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]]
答案 1 :(得分:0)
mat = range(1, 69)
[[elt]*len(mat) for elt in mat]
def repmat(mat):
return [[elt]*len(mat) for elt in mat]
repmat([0, 1, 2, 3])
[[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]
repmat2 = lambda mat:[[elt]*len(mat) for elt in mat]
repmat2([0, 1, 2, 3])
答案 2 :(得分:0)
尝试:
np.tile(np.reshape(y_1,(68,1)),(1,68))