我正在用for-loop生成数组(技术上说是行向量),a,b,c...是输出。
我可以将新的数组添加到旧的数组中一起形成一个矩阵吗?
import numpy as np
# just for example:
a = np.asarray([2,5,8,10])
b = np.asarray([1,2,3,4])
c = np.asarray([2,3,4,5])
... ... ... ... ...
我已经尝试过 ab = np.stack((a,b))
这样就可以了。但我的想法是在一个新的循环中向旧的矩阵中添加一条新的行,但要用 abc = np.stack((ab,c))
那么就会出现错误 ValueError: all input arrays must have the same shape
. 有谁能告诉我如何在已有的矩阵中添加另一个向量?我在这个论坛上找不到一个完美的答案。
感谢大家的想法,这个问题的最佳解决方案是在这个问题上添加 nparray
或 list
迭代时将其转换为一个列表,并使用 np.asarray
最后。
a = np.asarray([2,5,8,10]) # or a = [2,5,8,10]
b = np.asarray([1,2,3,4]) # b = [1,2,3,4]
c = np.asarray([2,3,4,5]) # c = [2,3,4,5]
... ...
l1 = []
l1.append(a)
l1.append(b)
l1.append(c)
... ...
l1
然而,这些元素不一定是空的。l1
已经包含的类型应与 a
,b
,c
例如,以下两者之间的区别 l1 = [1,1,1,1]
和 l1 = [[1,1,1,1]]
在这种情况下,是巨大的。