资源描述
*,/,38,安徽财经大学,Anhui University of Finance&Economics,1959,Matlab,实验,(二),第一讲 矩阵的基本运算,第二讲 向量的距离与夹角余弦,第三讲 数据的属性与处理方法,第二讲 向量的距离与夹角余弦,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,安徽财经大学,Anhui University of Finance&Economics,1.3 向量的距离与夹角余弦,1.4 线性方程组AX=b的求解,1.,向量的数量积,矢量积,例如:,a=,1,2,3,b,=-1,5,6,c=1,0,1,则,Matlab,中数量积,:,dot(,a,b,);,矢量积,:,cross(,a,b,),dot,(,a,b,)=27,cross,(,a,c,)=(2,2,-,2),解:,a,b,c,的混合积为:,dot,(,a,cross,(,b,c,),练习:计算,a,b,c,的混合积,三、向量的距离与夹角余弦,1,),Matlab,中向量,a,的范数为:,norm,(,a,),例,1,a=,1,2,3,b,=-1,5,6,c=1,0,1,求,a,b,的范数,解:,norm,(,a,)=3.7417,norm(,b,)=7.8740,练习:,对例,1,计算:,a,b,夹角的余弦,dot(a/norm(a),b/norm(b),解法二:,dot(a,b)/norm(a)/norm(b),解法一:,=0.9164,思考:,a,b,c,三个向量那两个更接近?,事实上,范数的平方,=,向量,a,自身的数量积,三、向量的距离与夹角余弦,2.,矩阵的范数与向量的标准化,如例,1,a=,1,2,3,b,=-1,5,6,c=1,0,1,求,a,b,c,之间的夹角余弦,解:输入,:A=a;b;c;,B=,1,-pdist,(,A,cosine,),输出结果为,:B=0.9164 0.7559 0.4490,三、向量的距离与夹角余弦,计算向量之间夹角的余弦还可以用命令,:,B=,1,-pdist,(,A,cosine,),计算矩阵,A,的行向量之间的夹角余弦,2),矩阵的范数有以下几种:,(1),n,=norm(,A,),矩阵,A,的普范数,(2,范数,),=,AA,的最大特征值的算术根,.,(2),n,=norm(,A,1),矩阵,A,的列范数(,1-,范数),等 于,A,的最大列之和,.,(3),n,=,norm(,A,inf,),矩阵,A,的行范数,(,无穷大范数,),等于,A,的最大行之和,.,(4),n,=norm(,A,fro),矩阵,A,的,Frobenius,范数,.,记为:,三、向量的距离与夹角余弦,3),方阵的谱半径:,方阵,A,的特征值的绝对值之最大值称为,A,的谱半径 记为:,例,3.,求矩阵 的,谱半径,由,eig(A,),知矩阵,A,的特征值分别为,1,-2,1,。,三、向量的距离与夹角余弦,例,3.,将矩阵,的行向量与列向量标准化,解:,A=1,2,3;4,5,6;7,8,0;B=,normr(A,),,,C=,normc(A,),也可以输入命令:,b(1)=norm(A(1,:);b(2)=norm(A(2,:);b(3)=norm(A(3,:);c=b*ones(1,3);B=A./c,4,),矩阵的行向量、列向量标准化的命令:,normr(A,),,,normc(A,),(,normr(A,),表示将矩阵每一行除以该行的范数),什么意思,?,求出,A,矩阵个各行的范数,转置后变为,3*1,阶矩阵,三、向量的距离与夹角余弦,n,维欧氏空间,:,设 表示,n,维向量,的全体所组成的集合,称为,n,维欧氏空间,称为 与 的,欧氏距离,称为 与 的,绝对距离,如果,三、向量的距离与夹角余弦,2.,常见的向量距离,闵可夫斯基距离,:,当,r=1,2,时分别为绝对距离和欧氏距离,马氏距离,:,其中,V,是一个实对称正定矩阵,通常取样本的协方差矩阵,当,V=E,时即为欧氏距离,.,以上距离,在,Matlab(6.),中有命令,:,pdist,具体如下:,三、向量的距离与夹角余弦,(1),欧氏距离,:,如果,A,是,a,m,阶矩阵,B,是,m,b,阶矩阵,.,即,A,的行向量维数等于,B,的列向量维数,三、向量的距离与夹角余弦,dist(A,B),结果是一个,a,b,阶上三角形矩阵,d(,i,j,),表示,A,的第,i,个,行向量与,B,的第,j,个列向量之间欧氏距离,Matlab,中命令:,dist(A,B),计算,A,中,每个行向量,与,B,中,每个列向量,之间欧氏距离,例,4.,a=,1,2,3,b,=-1,5,6,c=1,0,1,求,a,b,c,欧氏距离,解,:,输入,:a1=,dist(a,b),a2,=dist(,a,c,),a3=dist(c,b,),或者输入,:A=,a;b;c;pdist(A,),三、向量的距离与夹角余弦,Pdist(,X,),样本,X,中各,n,维向量的欧氏距离,如果,X,是,m,个,n,维行向量所组成的矩阵,则有:,注意:而,pdist(X,),是个一行 列,矩阵。各列分别表示,X,中各行向量按如下顺序的距离,(1,2),(1,3),(1,m),(2,3),(2,4),(2,m),(m-1,m),(2),绝对距离:,Matlab,中命令:,mandist(A,B,),计算,A,中每个行向量与,B,中每个列向量之间绝对距离,,A,的行向量维数必须等于,B,的列向量维数,.,三、向量的距离与夹角余弦,设样本,X,是,m,个,n,维行向量所组成的矩阵,则有:,Pdist(,X,cityblock,),各,n,维向量的绝对距离,注意:而,pdist(X,),是个一行 列,矩阵。各列分别表示,X,中各行向量按如下顺序的距离,(1,2),(1,3),(1,m),(2,3),(2,4),(2,m),(m-1,m),例,5.,求例,2,中向量之间的绝对距离,.,mandist(a,b,)=8,;,mandist(a,c,)=4,;,mandist(c,b,)=12,解,:,dist(,a,b,)=4.6904,dist(,a,c,)=2.8284,dist(c,b,)=7.3485,还可以用什么命令,?,你发现了什么?,三、向量的距离与夹角余弦,与,绝对距离比较,设样本,X,是,m,个,n,维行向量所组成的矩阵,则有:,Pdist(,X,Minkowski,,,r,),闵可夫斯基距离,Pdist(,X,mahal,),各,n,维向量的马氏距离,注意:而,pdist(X,),是个一行 列,矩阵。各列分别表示,X,中各行向量按如下顺序的距离,(1,2),(1,3),(1,m),(2,3),(2,4),(2,m),(m-1,m),三、向量的距离与夹角余弦,(3),闵可夫斯基距离与马氏距离,例,6.,现测得,6,只,Apf,和,9,只,Af,蠓虫的触长,翅长数据如下:,Apf,:,(1.14,1.78),(1.18,1.96),(1.20,1.86),(1.26,2.00),(1.28,2.00),(1.30,1.96),Af,:,(1.24,1.72),(1.36,1.74),(1.38,1.64),(1.38,1.82),(1.38,1.90),(1.40,1.70),(1.48,1.82),(1.54,1.82),(1.56,2.08),计算两类蠓虫的各自之间的欧氏、绝对、马氏距离,解,:,输入,Af,=1.24,1.72;1.36,1.74;1.38,1.64;1.38,1.82;1.38,1.90;1.40,1.70;1.48,1.82;1.54,1.82;1.56,2.08,;,Apf,=1.14,1.78;1.18,1.96;1.2,1.86;1.26,2.;1.28,2;,1.30,1.96;,三、向量的距离与夹角余弦,d1=(,pdist(Apf,);,d2=(,pdist(Apf,cityblock,);,d3=,pdist(Apf,mahal,);,d=d1,d2,d3,输出结果为,Apf,蠓虫之间的各类距离为含有,15,个元素的列向量,将输出结果变化为,15,行,3,列的矩阵,结果见表,1,同理可以求得,Af,蠓虫之间的各类距离。结果见表,2,三、向量的距离与夹角余弦,Apf,蠓虫,欧氏距离,绝对距离,马氏距离,d12,0.1844,0.2200,2.5626,d13,0.1000,0.1400,0.9883,d14,0.2506,0.3400,2.4942,d15,0.2608,0.3600,2.5318,d16,0.2408,0.3400,2.5478,d23,0.1020,0.1200,2.2507,d24,0.0894,0.1200,1.5470,d25,0.1077,0.1400,2.0430,d26,0.1200,0.1200,3.0777,d34,0.1523,0.2000,1.6534,d35,0.1612,0.2200,1.5873,d36,0.1414,0.2000,1.6025,d45,0.0200,0.0200,0.5129,d46,0.0566,0.0800,1.6616,d56,0.0447,0.0600,1.1764,表一.Apf蠓虫之间的距离,Af,蠓,欧氏距,绝对距,马氏距,Af,蠓,欧氏距,绝对距,马氏距,d12,0.1217,0.1400,1.4423,d37,0.2059,0.2800,1.3971,d13,0.1612,0.2200,2.3963,d38,0.2408,0.3400,1.6847,d14,0.1720,0.2400,1.4225,d39,0.4754,0.6200,3.4103,d15,0.2280,0.3200,1.5517,d45,0.0800,0.0800,0.7917,d16,0.1612,0.1800,2.2078,d46,0.1217,0.1400,1.3659,d17,0.2600,0.3400,2.6110,d47,0.1000,0.1000,1.2987,d18,0.3162,0.4000,3.3635,d48,0.1600,0.1600,2.0780,d19,0.4817,0.6800,3.3694,d49,0.3162,0.4400,2.1271,d23,0.1020,0.1200,1.1705,d56,0.2010,0.2200,2.1520,d24,0.0825,0.1000,0.6601,d57,0.1281,0.1800,1.8990,d25,0.1612,0.1800,1.4345,d58,0.1789,0.2400,2.6482,d26,0.0566,0.0800,0.8277,d59,0.2546,0.3600,1.8449,d27,0.1442,0.2000,1.2266,d67,0.1442,0.2000,0.9689,d28,0.1970,0.2600,1.9404,d68,0.1844,0.2600,1.4149,d29,0.3945,0.5400,2.6612,d69,0.4123,0.5400,2.9389,d34,0.1800,0.1800,1.7814,d78,0.0600,0.0600,0.7792,d35,0.2600,0.2600,2.5731
展开阅读全文