天天看点

python numpy--矩阵的运算

1.加减乘

python numpy--矩阵的运算
#创建两个矩阵
a = np.mat(np.array([2,6,5]))
b = np.mat(np.array([1,2,3]))
# add 
a+b #直接用加法
np.add(a,b) #使用加法函数
# subtract 
a-b #直接用减法
np.subtract(a,b) #使用减法函数 谁在前面谁是被减数
# multiply 
np.multiply(a,b) #使用乘法函数,直接相乘是没有结果的,因为第一个矩阵的行和第二个矩阵的行不相同

           

2.除法运算

python numpy--矩阵的运算
# divide 
a/b #直接相除
np.divide(a,b)  #使用除法函数
# floor_divide
a//b #向下取整 坐标轴左边
np.floor_divide(a,b) #  ==a//b 使用的是函数


           

3.模运算

#创建一个数组
m = np.mat(np.arange(-4,4))
           
print('m:\t\t',m)
print('remainder:\t',np.remainder(m,2))
print('mod:\t\t',np.mod(m,2))
print('%:\t\t',m%2)
print('fmod:\t\t',np.fmod(m,2))     #正负数由被除数决定
           
m:		 [[-4 -3 -2 -1  0  1  2  3]]
remainder:	 [[0 1 0 1 0 1 0 1]]
mod:		 [[0 1 0 1 0 1 0 1]]
%:		 [[0 1 0 1 0 1 0 1]]
fmod:		 [[ 0 -1  0 -1  0  1  0  1]]