numpy拼接矩阵的实现
作者:头戴面包圈 时间:2021-02-09 06:35:07
1、文档
使用numpy的 concatenate 拼接矩阵,文档里面这样解释:
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
(a1, a2, ...):连接的数组必须有一样的维度;
axis:拼接的方向;
out:预设输出矩阵的大小
…………
2、举例
首先给定两个矩阵:
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.]]
官方文档地址:numpy官网地址
来源:https://blog.csdn.net/Arhonbt/article/details/125746727
标签:numpy,拼接,矩阵
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用百度翻译进行中翻英示例
2021-04-08 20:10:11
python 字符串和整数的转换方法
2023-10-11 02:31:42
python颜色随机生成器的实例代码
2022-12-03 19:40:05
在python image 中安装中文字体的实现方法
2021-08-06 06:19:35
border-radius与圆角
2008-12-29 14:05:00
![](https://img.aspxhome.com/file/UploadPic/200812/29/corner-92s.png)
基于python批量处理dat文件及科学计算方法详解
2021-05-16 18:00:12
![](https://img.aspxhome.com/file/2023/5/99135_0s.jpg)
pycharm 2018 激活码及破解补丁激活方式
2021-10-16 12:10:00
![](https://img.aspxhome.com/file/2023/0/120910_0s.png)
tensorflow 重置/清除计算图的实现
2023-07-23 00:41:58
PHP实战之投票系统的实现
2023-10-24 10:40:39
![](https://img.aspxhome.com/file/2023/5/55565_0s.jpg)
python中pytest收集用例规则与运行指定用例详解
2021-12-01 14:23:21
CSS制作11种风格不同的特效文字
2010-10-20 20:08:00
如何利用pandas将Excel转为html格式
2023-12-14 19:04:49
![](https://img.aspxhome.com/file/2023/7/121047_0s.png)
Python操作RabbitMQ服务器实现消息队列的路由功能
2022-06-21 00:50:39
Flask框架踩坑之ajax跨域请求实现
2023-12-25 01:28:02
![](https://img.aspxhome.com/file/2023/3/80703_0s.png)
python用正则表达式提取/匹配中文汉字
2021-06-12 23:45:50
![](https://img.aspxhome.com/file/2023/3/99303_0s.jpg)
Python发送邮件封装实现过程详解
2021-06-13 09:19:41
![](https://img.aspxhome.com/file/2023/4/118744_0s.png)
深入浅析python中的多进程、多线程、协程
2022-06-05 15:59:27
Python 数据结构之树的概念详解
2021-06-24 17:08:25
![](https://img.aspxhome.com/file/2023/8/78798_0s.png)
互联网产品设计零碎记
2010-09-25 12:49:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/diedai-600x64-87s.png)
Python爬取酷狗MP3音频的步骤
2022-04-07 03:15:21