矩阵的构造
- 零矩阵:
zeros()
- 一矩阵:
ones()
- 单位矩阵:
eye()
- 均匀分布随机数矩阵
[0,1]区间:rand(n)
orrand(n,m)
[a,b]区间:rand(n)*(b-a)+a
orrand(n,m)*(b-a)+a
- 正态分布随机数矩阵
均值=0,标准差为1:randn(n)
orrandn(n,m)
均值=a,标准差为b:b*rand(n)+a
orb*rand(n,m)+a
实例化:均值=5,标准差为10的10000个数据点。
>> tt=10*randn(10000,1)+5;
>> mean(tt)
ans =
4.9791
>> std(tt)
ans =
9.9932
- 对角矩阵:
diag()
- 稀疏矩阵:
sparse()
实例化:
定义1个10*10的稀疏矩阵,非零元素为(1,1)=1,(2,2)=2,(3,2)=5
>> SB=sparse([1,2,3],[1,2,2],[1,2,5],10,10);
>> SB
SB =
(1,1) 1
(2,2) 2
(3,2) 5
>> full(SB)
ans =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 5 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
矩阵运算
矩阵指数运算
函数:expm(X)
实例:
sigma_x=[0,1;1,0];
Rx=@(theta)expm(-1*1j*sigma_x*theta/2);
>> Rx(pi)
ans =
-0.0000 + 0.0000i 0.0000 - 1.0000i
0.0000 - 1.0000i -0.0000 + 0.0000i
常用函数
- 获得矩阵的a*b信息
[row,col]=size(A)