2022年matlab编写程序 .pdf
《2022年matlab编写程序 .pdf》由会员分享,可在线阅读,更多相关《2022年matlab编写程序 .pdf(42页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、mathematics Basic Matrix Operations a=1 2 3 4 5生成矩阵; b=a+2 矩阵加上数字 plot(b)画三点图 grid on生成网格 bar(b)生成条状图 xlabel(sample#) 给 X轴加标注 ylabel(pound) 给 Y 轴加标注 title(bar plot)加标题 plot(b,*)用*表示点 axis(0 10 10 20 0 20)各个轴的范围 A = 1 2 0; 2 5 -1; 4 10 -1 B=A 转置 C=A*B 矩阵相乘 C=A.*B 数组相乘 X=inv(A)逆 I=inv(A)*A单位矩阵 eig(A)特
2、征值 svd(A) the singular value decomposition. 奇异值分解名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 42 页 - - - - - - - - - p = round(poly(A)生成特征多项式的系数 roots(p) 特征多项式的根,即矩阵的特征值 q = conv(p,p) 向量的卷积 r = conv(p,q) 再向量的卷积 plot(r) who 变量列表 whos 变量的详细列表 sqrt(-1); sqrt(2)
3、开方Matrix Manipulation A = magic(3) 生成魔方矩阵graphics 2-D Plots Line Plot of a Chirp(线状图) doc plot 在帮助中查看plot更多的信息 x=0:0.05:5; y=sin(x.2); plot(x,y); plot(x,y,o); xlabel(Time) ylabel(Amplitude) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 42 页 - - - - - - - - - B
4、ar Plot of a Bell Shaped Curve(条状图) x = -2.9:0.2:2.9; bar(x,exp(-x.*x);函数中包括了计算,plot直接完成Stairstep Plot of a Sine Wave(阶梯图) x=0:0.25:10; stairs(x,sin(x); Errorbar Plot x=-2:0.1:2; y=erf(x); y=erf(x);e = rand(size(x)/10 errorbar(x,y,e); Polar Plot t=0:0.01:2*pi; polar(t,abs(sin(2*t).*cos(2*t); abs绝对值S
5、tem Plot x = 0:0.1:4; y = sin(x.2).*exp(-x); stem(x,y) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 42 页 - - - - - - - - - Scatter Plot load count.dat scatter(count(:,1),count(:,2),r*) xlabel(Number of Cars on Street A); ylabel(Number of Cars on Street B); 更多
6、 plot的类型可以在命令行输入doc praph2d查看3-D Plots Mesh Plot of Peaks z=peaks(25);peaksis a function of two variables, obtained by translating and scaling Gaussian distributions, which is useful for demonstrating mesh, surf, pcolor, contour, and so on. mesh(z); meshc(z) ;带有轮廓线 colormap(hsv); 对 mesh的图象使用不同的色彩, 如
7、 gray, hot, bone. Etc Surface Plot of Peaks z=peaks(25); surf(z); 3-D shaded surface plot surfc(z);带有轮廓线 colormap jet;同 colormap(jet); 对 mesh的图象使名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 42 页 - - - - - - - - - 用不同的色彩Surface Plot (with Shading) of Peaks z=p
8、eaks(25); surfl(z); Surface plot with colormap-based lighting shading interp; Set color shading properties colormap(pink); Contour Plot of Peaks z=peaks(25); contour(z,16) 轮廓线 colormap(hsv) Quiver (Quiver or velocity plot) x = -2:.2:2; y = -1:.2:1; xx,yy = meshgrid(x,y); zz = xx.*exp(-xx.2-yy.2); px
9、,py = gradient(zz,.2,.2); quiver(x,y,px,py,2); Slice (Volumetric slice plot) x,y,z = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 42 页 - - - - - - - - - v = x.*exp(-x.2-y.2-z.2); xslice = -1.2,.8,2; yslice = 2; zslice = -2
10、,0; x,y,z = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2); slice(x,y,z,v,xslice,yslice,zslice) colormap hsv Programming Manipulating Multidimensional Arrays Creating Multi-Dimensional Arrays The CAT function is a useful tool for building multidimensional arrays. B = cat(DIM,A1,A2,.) builds a multidimensional
11、array by concatenating A1, A2 . along the dimension DIM. Calls to CAT can be nested. B = cat( 3, 2 8; 0 5, 1 3; 7 9, 2 3; 4 6); B(:,:,1) = 2 8 0 5 B(:,:,2) = 1 3 7 9 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 42 页 - - - - - - - - - B(:,:,3) = 2 3 4 6 A = ca
12、t(3,9 2; 6 5, 7 1; 8 4); B = cat(3,3 5; 0 1, 5 6; 2 1); C = cat(4,A,B,cat(3,1 2; 3 4, 4 3; 2 1); C(:,:,1,1) = 9 2 6 5 C(:,:,2,1) = 7 1 8 4 C(:,:,1,2) = 3 5 0 1 C(:,:,2,2) = 5 6 2 1 C(:,:,1,3) = 1 2 3 4 C(:,:,2,3) = 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共
13、 42 页 - - - - - - - - - 4 3 2 1 A = cat(2,9 2; 6 5, 7 1; 8 4); B = cat(2,3 5; 0 1, 5 6; 2 1); C = cat(4,A,B,cat(2,1 2; 3 4, 4 3; 2 1) C(:,:,1,1) = 9 2 7 1 6 5 8 4 C(:,:,1,2) = 3 5 5 6 0 1 2 1 C(:,:,1,3) = 1 2 4 3 3 4 2 1 C = cat(2,A,B,cat(2,1 2; 3 4, 4 3; 2 1) C = 9 2 7 1 3 5 5 6 1 2 4 3; 6 5 8 4 0
14、1 2 1 3 4 2 1 C = cat(1,A,B,cat(2,1 2; 3 4, 4 3; 2 1) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 42 页 - - - - - - - - - C = 9 2 7 1 6 5 8 4 3 5 5 6 0 1 2 1 1 2 4 3 3 4 2 1 Finding the Dimensions SzA = size(A) DimsA = ndims(A) SzC = size(C) DimsC = ndims(C)
15、Accessing Elements K = C(:,:,1,1 3) K(:,:,1,1) = 9 2 6 5 K(:,:,1,2) = 1 2 3 4 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 42 页 - - - - - - - - - Manipulating Multi-Dimensional Arrays A = rand(3,3,2); B = permute(A, 2 1 3); C = permute(A, 3 2 1); Selecting 2D
16、 Matrices From Multi-Dimensional Arrays A = cat( 3, 1 2 3; 9 8 7; 4 6 5, 0 3 2; 8 8 4; 5 3 5, . 6 4 7; 6 8 5; 5 4 3); % The EIG function is applied to each of the horizontal slices of A. for i = 1:3 eig(squeeze(A(i,:,:) end x1 = -2*pi:pi/10:0; x2 = 2*pi:pi/10:4*pi; x3 = 0:pi/10:2*pi; x1,x2,x3 = ndgr
17、id(x1,x2,x3); z = x1 + exp(cos(2*x2.2) + sin(x3.3); slice(z,5 10 15, 10, 5 12); axis tight; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 42 页 - - - - - - - - - Structures % Draw a visualization of a structure. strucdem_helper(1) patient.name = John Doe; pati
18、ent.billing = 127.00; patient.test = 79 75 73; 180 178 177.5; 172 170 169; patient patient = name: John Doe billing: 127 test: 3x3 double patient(2).name = Ann Lane; patient(2).billing = 28.50; patient(2).test = 68 70 68; 118 118 119; 172 170 169; % Update the visualization. strucdem_helper(2); fnam
19、es1 = fieldnames(patient) patient2 = rmfield(patient,test); fnames2 = fieldnames(patient2) fnames1 = name 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 42 页 - - - - - - - - - billing test fnames2 = name billing A = struct( data, 3 4 7; 8 0 1, 9 3 2; 7 6 5, .
20、nest, . struct( testnum, Test 1, . xdata, 4 2 8, ydata, 7 1 6), . struct( testnum, Test 2, . xdata, 3 4 2, ydata, 5 0 9) % Update the visualization. strucdem_helper(3) 聚类分析K(C)均值算法:x=-7.82 -4.58 -3.97;-6.68 3.16 2.17;4.36 -2.19 2.09;6.72 0.88 2.80;. -8.64 3.06 3.5;-6.87 0.57 -5.45;4.47 -2.62 5.76;6.
21、73 -2.01 4.18;. -7.71 2.34 -6.33;-6.91 -0.49 -5.68;6.18 2.81 5.82;6.72 -0.93 -4.04;. -6.25 -0.26 0.56;-6.94 -1.22 1.13;8.09 0.20 2.25;6.81 0.17 -4.15;. -5.19 4.24 4.04;-6.38 -1.74 1.43;4.08 1.3 5.33;6.27 0.93 -2.78;idx,ctrs=kmeans(x,3) 如何编写求 K-均值聚类算法的Matlab 程序名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - -
22、 - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 42 页 - - - - - - - - - 在聚类分析中, K-均值聚类算法( k-means algorithm)是无监督分类中的一种基本方法,其也称为C-均值算法,其基本思想是:通过迭代的方法,逐次更新各聚类中心的值,直至得到最好的聚类结果。假设要把样本集分为c 个类别,算法如下:(1)适当选择 c 个类的初始中心;(2)在第 k 次迭代中,对任意一个样本,求其到c 个中心的距离,将该样本归到距离最短的中心所在的类,(3)利用均值等方法更新该类的中心值;(4)对于所有的 c 个聚类中心,如果利用
23、(2)(3)的迭代法更新后,值保持不变,则迭代结束,否则继续迭代。下面介绍作者编写的一个分两类的程序,可以把其作为函数调用。% function samp1,samp2=kmeans(samp); 作为调用函数时去掉注释符samp=11.1506 6.7222 2.3139 5.9018 11.0827 5.7459 13.2174 13.8243 4.8005 0.9370 12.3576; %样本集l0 l=size(samp); %利用均值把样本分为两类,再将每类的均值作为聚类中心th0=mean(samp); n1=0;n2=0; c1=0.0; c1=double(c1); c2=c
24、1; for i=1:l if samp(i)th0 c1=c1+samp(i);n1=n1+1; else c2=c2+samp(i);n2=n2+1; end end c1=c1/n1;c2=c2/n2; %初始聚类中心t=0; cl1=c1;cl2=c2; c11=c1;c22=c2; %聚类中心while t=0 samp1=zeros(1,l); samp2=samp1; n1=1;n2=1; for i=1:l if abs(samp(i)-c11) load hald covx=cov(ingredients) 名师资料总结 - - -精品资料欢迎下载 - - - - - - -
25、 - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 42 页 - - - - - - - - - pc, latent, explained=pcacov(covx) pc, score, latent, tsquare= princomp (X) pcthe principal component coefficients The scores are the data formed by transforming the original data into the space of the principal components.T
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年matlab编写程序 2022 matlab 编写 程序
限制150内