我读过以下相关的讨论What's the simplest way to extend a numpy array in 2 dimensions?
但是,如果我想要扩展多个矩阵,例如
A = np.matrix([[1,2],[3,4]])
B = np.matrix([[3,4],[5,6]])
C = np.matrix([[7,8],[5,6]])
F = np.append(A,[[B]],0)然而,python说
ValueError:所有输入数组必须具有相同的维数,但索引0处的数组具有二维,而索引1处的数组具有4维。
我想把B放在矩阵A下面,把C放在矩阵B下面。
所以,F应该是一个6X2矩阵。
怎么做?谢谢!
发布于 2020-12-22 19:33:36
尝试使用numpy.concatenate (https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html):
A = np.matrix([[1,2],[3,4]])
B = np.matrix([[3,4],[5,6]])
C = np.matrix([[7,8],[5,6]])
# F = np.append(A,[[B]],0)
F = np.concatenate((A, B, C), axis=1)将axis参数更改为0,以组合矩阵‘垂直’:
print(np.concatenate((A, B, C), axis=1))[1 2 3 4 7 8]
print(np.concatenate((A, B, C), axis=0))[1 2]
3 4
7[ 8]
发布于 2020-12-22 19:33:28
我认为np.concatenate应该能做到这一点
A = np.matrix([[1,2],[3,4]])
B = np.matrix([[3,4],[5,6]])
C = np.matrix([[7,8],[5,6]])
ABC = np.concatenate([A,B,C],axis = 0) # axis 0 stacks it one above the other
print("Shape : ",ABC.shape)
print(ABC)产出:
Shape : (6, 2)
matrix(
[[1, 2],
[3, 4],
[3, 4],
[5, 6],
[7, 8],
[5, 6]])https://stackoverflow.com/questions/65414216
复制相似问题