python如何进行矩阵运算
作者:yang 时间:2021-05-01 22:58:25
python进行矩阵运算的方法:
1、矩阵相乘
>>>a1=mat([1,2]);
>>>a2=mat([[1],[2]]);
>>>a3=a1*a2 #1*2的矩阵乘以2*1的矩阵,得到1*1的矩阵
>>> a3
matrix([[5]])
2、矩阵对应元素相乘
>>>a1=mat([1,1]);
>>>a2=mat([2,2]);
>>>a3=multiply(a1,a2)
>>> a3
matrix([[2, 2]])
multiply()函数:数组和矩阵对应位置相乘,输出与相乘数组/矩阵的大小一致
3、矩阵点乘
>>>a1=mat([2,2]);
>>>a2=a1*2
>>>a2
matrix([[4, 4]])
4、矩阵求逆
>>>a1=mat(eye(2,2)*0.5)
>>> a1
matrix([[ 0.5, 0. ],
[ 0. , 0.5]])
>>>a2=a1.I #求矩阵matrix([[0.5,0],[0,0.5]])的逆矩阵
>>> a2
matrix([[ 2., 0.],
[ 0., 2.]])
5、矩阵转置
>>> a1=mat([[1,1],[0,0]])
>>> a1
matrix([[1, 1],
[0, 0]])
>>> a2=a1.T
>>> a2
matrix([[1, 0],
[1, 0]])
6、计算每一列、行的和
>>>a2=a1.sum(axis=0) #列和,这里得到的是1*2的矩阵
>>> a2
matrix([[7, 6]])
>>>a3=a1.sum(axis=1) #行和,这里得到的是3*1的矩阵
>>> a3
matrix([[2],
[5],
[6]])
>>>a4=sum(a1[1,:]) #计算第一行所有列的和,这里得到的是一个数值
>>> a4
5 #第0行:1+1;第2行:2+3;第3行:4+2
内容扩展:
numpy矩阵运算
(1) 矩阵点乘:m=multiply(A,B)
(2) 矩阵乘法:m1=a*b m2=a.dot(b)
(3) 矩阵求逆:a.I
(4) 矩阵转置:a.T
来源:https://www.py.cn/faq/python/18649.html
标签:python,矩阵运算
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
纯CSS实现导航下拉菜单
2007-11-25 15:11:00
![](https://img.aspxhome.com/file/UploadPic/200711/25/2007112515291686s.gif)
php结合js实现点击超链接执行删除确认操作
2023-11-15 03:30:51
python+requests+pytest接口自动化的实现示例
2022-11-01 06:12:27
![](https://img.aspxhome.com/file/2023/2/64302_0s.jpg)
Python高级特性 切片 迭代解析
2022-03-28 14:32:49
![](https://img.aspxhome.com/file/2023/5/84915_0s.png)
asp查询xml的代码 不刷新页面查询的方法
2011-04-06 11:00:00
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
2022-05-14 00:35:25
![](https://img.aspxhome.com/file/2023/7/70227_0s.png)
win10环境下配置vscode python开发环境的教程详解
2022-09-06 21:19:49
![](https://img.aspxhome.com/file/2023/1/71991_0s.png)
Python之日期与时间处理模块(date和datetime)
2023-09-29 12:53:06
![](https://img.aspxhome.com/file/2023/2/63232_0s.png)
python网络爬虫基于selenium爬取斗鱼直播信息
2023-03-05 09:33:19
Python之根据输入参数计算结果案例讲解
2022-08-27 07:44:50
![](https://img.aspxhome.com/file/2023/9/69499_0s.jpg)
asp 隐藏并修改文件的最后修改时间
2011-03-29 10:34:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112319598.jpg)
VS 2010 Ultimate架构代码探索
2010-05-02 20:38:00
![](https://img.aspxhome.com/file/UploadPic/20105/2/201052204722882s.jpg)
解析SQL Server数据体系和应用程序逻辑
2009-01-23 13:58:00
Python3列表删除的三种方式实现
2021-03-26 17:55:55
![](https://img.aspxhome.com/file/2023/5/89305_0s.jpg)
15个短代码示例理解python丰富的编程思维
2022-07-18 08:16:57
python虚拟机之描述器实现原理与源码分析
2022-05-24 14:41:10
PHP register_shutdown_function()函数的使用示例
2023-10-07 08:06:27
关于matplotlib-legend 位置属性 loc 使用说明
2021-12-19 06:48:25
![](https://img.aspxhome.com/file/2023/3/78513_0s.jpg)
分析Python感知线程状态的解决方案之Event与信号量
2022-05-11 07:23:35
![](https://img.aspxhome.com/file/2023/3/76783_0s.jpg)
python flask之模板继承方式
2022-05-26 03:38:24
![](https://img.aspxhome.com/file/2023/2/89162_0s.png)