使用numpy的 concatenate 拼接矩阵,文档里面这样解释:
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
(a1, a2, ...):连接的数组必须有一样的维度;
axis:拼接的方向;
out:预设输出矩阵的大小
…………
首先给定两个矩阵:
rotation = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
trans = np.array([[7],
[8],
[0]])
z = np.concatenate((rotation, trans), axis=1)
输出为:
[[1 2 3 7]
[4 5 6 8]
[7 8 9 0]]
add_arr = np.array([[0, 0, 0, 0]])
array_by_add = np.concatenate((z, add_arr), axis=0)
输出:
[[1 2 3 7]
[4 5 6 8]
[7 8 9 0]
[0 0 0 0]]
第一个为3×3矩阵,第二个为2×3矩阵。
arr1 = np.array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
arr2 = np.array([[7, 8, 9],
[8, 9, 10]])
arr_zeros1 = np.zeros((arr2.shape[1], arr1.shape[0]))
print(arr_zeros1)
arr_zeros2 = np.zeros((arr2.shape[0], arr1.shape[1]))
print(arr_zeros2)
total_arr1 = np.concatenate((arr1, arr_zeros1), axis=1)
total_arr2 = np.concatenate((arr_zeros2, arr2,), axis=1)
total_arr = np.concatenate((total_arr1, total_arr2), axis=0)
print(total_arr)
拼接之后结果如下:
[5 6]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[0. 0. 0.]
[0. 0. 0.]]
[[ 2. 3. 4. 0. 0. 0.]
[ 3. 4. 5. 0. 0. 0.]
[ 4. 5. 6. 0. 0. 0.]
[ 0. 0. 0. 7. 8. 9.]
[ 0. 0. 0. 8. 9. 10.]]