python和matlab计算速度对比_MATLAB与NumPy的对比
文章源自《NumPy for MATLAB user》。**本文目录[TOC]1. 算术运算MATLABPython描述备注a.^bnp.power(a,b)a**ba的b次方rem(a,b)a % bnp.remainder(a,b)np.fmod(a,b)取余,模运算factorial(a)np.math.factorial(a)math.factorial(a)a的阶乘math是未经优化的P
文章源自《NumPy for MATLAB user》。**
本文目录
[TOC]
1. 算术运算
MATLAB
Python
描述
备注
a.^b
np.power(a,b)
a**b
a的b次方
rem(a,b)
a % b
np.remainder(a,b)
np.fmod(a,b)
取余,模运算
factorial(a)
np.math.factorial(a)
math.factorial(a)
a的阶乘
math是未经优化的Python标准库,而np.math是经过优化的,速度相对更快。
2. 关系运算
MATLAB
Python
描述
备注
a ~= b
a != b
判断a和b是否不等
3. 逻辑运算
MATLAB
Python
描述
备注
a && b
a and b
单一元素与运算
只适应一个元素
a || b
a or b
单一元素或运算
只适应一个元素
a & b
and(a,b)
np.logical_and(a,b)
a and b
多元素与运算
a | b
or(a,b)
np.logical_or(a,b)
a or b
多元素或运算
xor(a,b)
np.logical_xor(a,b)
异或运算
~a
not(a)
np.logical_not(a)
not a
!a
非运算
适用对象待更新
any(a)
any(a)
存在非零元素就返回true
len(np.nonzero(a)[0])>0
all(a)
all(a)
所有元素不为零才返回true
len(np.nonzero(a)[0])>0
4. 根运算与对数运算
MATLAB
Python
描述
备注
sqrt(a)
math.sqrt(a)
np.sqrt(a)
平方根
MATLAB中一个数,默认是1*1的矩阵。所以MATLAB中对单元素和多元素处理是通用的。而Python中,数和数组在定义上是进行了区分的。此处自带的math标准库仅适用处理单一元素,NumPy中方法既适用于处理单元素(数),也适用于处理多元素(数组)。
log(a)
math.log(a)
np.log(a)
自然对数,底为e
同上
log10(a)
math.log10(a)
np.log10(a)
底数为10
同上
log2(a)
math.log(a,2)
np.log(a,2)
底数为2
同上
exp(a)
math.exp(a)
np.exp(a)
常数e的a次方
同上
5. 去尾运算
MATLAB
Python
描述
备注
round(a)
np.around(a)
round(a)
四舍五入
见例1
ceil(a)
math.ceil(a)
np.ceil(a)
向上(更大的数)取整,注意不是收尾法,因为要考虑负数
MATLAB和Python-math得到的是整数,Python得到的是处理了尾数的小数
floor(a)
math.floor(a)
np.floor(a)
向下(更小的数)取整,注意不是去尾法,因为要考虑负数
同上
fix(a)
np.fix(a)
向0取整
返回一个array
#例1-Python
>>> a = 9.8
>>> round(a)
10
>>> np.around(a)
10.0
%例1-MATLAB
>> a = 9.8
>> round(a)
ans =
10
6. 数学常量
MATLAB
Python
描述
备注
pi
math.pi
np.pi
pi = 3.141592653589793
exp(1)
math.e
math.exp(1)
np.e
np.exp(1)
e=2.718281828459045
e=2.718281828459045
e=2.718281828459045
e=2.7182818284590451
7. 向量
MATLAB
Python
描述
备注
a=[2 3 4 5]
a=np.array([2,3,4,5])
行向量
a'
a.T
a.reshape(-1,1)
向量的转置
8. 序列
MATLAB
Python
描述
备注
1:10
list(range(1,11))
np.arange(1,11)
MATLAB和Python1:[1,2,3,4,5,7,8,9,10]
Python2:array([1,2,3,4,5,6,7,8,9,10])
1:3:10
np.arange(1,11,3)
1,4,7,10
10:-1:1
np.arange(10,0,-1)
10,9,8,7,6,5,4,3,2,1
10:-3:1
np.arange(10,0,-3)
10,7,4,1
linspace(1,10,7)
np.linspace(1,10,7)
matlab: [1.0, 2.5, 4.0, 5.5, 7.0, 8.5, 10.0]
Python: array([1.0, 2.5, 4.0, 5.5, 7.0, 8.5, 10.0])
参数依次为:起点,终点,点的个数。此函数是将起点到终点之间的距离均匀分段。
a(:)=3
a.fill(3)
a[:]=3
将所有元素的值都赋为3
9. 拼接矩阵
MATLAB
Python
描述
备注
a=[1,2,3; 4,5,6]
a=np.array([[1,2,3],[4,5,6]])
[a,a]
np.concatenate((a,a), axis=1)
见例2
[a;a]
np.concatenate((a,a), axis=0)
见例2
例2-MATLAB
>> a=[1,2,3; 4,5,6]
a =
1 2 3
4 5 6
>> [a, a]
ans =
1 2 3 1 2 3
4 5 6 4 5 6
>> [a;a]
ans =
1 2 3
4 5 6
1 2 3
4 5 6
例2-Python
>>> a=np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> np.concatenate((a,a))
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
>>> np.concatenate((a,a), axis=0)
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
>>> np.concatenate((a,a), axis=1)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
附
该文章于2017年5月25日于CSDN上首次发表,2017年12月22日搬家至此!
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)