之前做数值实验用的MATLAB,MATLAB用矩阵来储存与操作数据特别方便,今天来学习一下python的矩阵运算——numpy库的mat方法。
#创建
>>> a=np.mat('1,2;3,4;5,6')
>>> a
matrix([[1, 2],
[3, 4],
[5, 6]])
#mat()函数中数据可以为字符串用分号分割或为列表形式用逗号分割。
>>> b=np.mat([[1,2],[3,4]])
>>> b
array([[1, 2],
[3, 4]])
#常见矩阵创建
#零矩阵——(3,3)为tuple类型
>>> k=np.mat(np.zeros((3,3)))
>>> k
matrix([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
#1矩阵
>>> k=np.mat(np.ones((3,3)))
>>> k
matrix([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
#随机整数矩阵,给定上界(0-10,3*3)
>>> k= np.mat(np.random.randint(10,size=(3,3)))
>>> k
matrix([[5, 6, 1],
[1, 4, 4],
[2,