把一个1X3的矩阵拼接在3X3的矩阵下方
随机生成两个矩阵
import numpy as np
a=np.random.randint(1,9,size=9).reshape((3,3))
print(a)
b=np.random.randint(1,9,size=3)
print(b)
结果
[[5 8 5]
[4 8 6]
[8 6 4]]
[5 3 7]
如果要把矩阵b拼接在a下方:
b=np.expand_dims(b,axis=0)
b
Out[98]: array([[5, 3, 7]])
b.shape
Out[99]: (1, 3)
np.concatenate((a,b),axis=0)
Out[100]:
array([[5, 8, 5],
[4, 8, 6],
[8, 6, 4],
[5, 3, 7]])
concatenate要求两个input矩阵要有同样数量的维度
如果不expand_dims会报错
```python
ValueError: all the input arrays must have same number of dimensions,
拼接在右侧
```python
np.concatenate((a,b.T),axis=1)
结果
array([[5, 8, 5, 5],
[4, 8, 6, 3],
[8, 6, 4, 7]])
用block函数也可以实现类似功能,请参见下期博客