数组及运算2课时.ppt

上传人:桔**** 文档编号:571129231 上传时间:2024-08-08 格式:PPT 页数:36 大小:475.50KB
返回 下载 相关 举报
数组及运算2课时.ppt_第1页
第1页 / 共36页
数组及运算2课时.ppt_第2页
第2页 / 共36页
数组及运算2课时.ppt_第3页
第3页 / 共36页
数组及运算2课时.ppt_第4页
第4页 / 共36页
数组及运算2课时.ppt_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《数组及运算2课时.ppt》由会员分享,可在线阅读,更多相关《数组及运算2课时.ppt(36页珍藏版)》请在金锄头文库上搜索。

1、Matlab及其应用重庆大学光电工程学院Matlab及其应用1MATLAB 及应用第四讲 数组及其运算Matlab及其应用2o在matlab中,矩阵(变量)都不必事先定义维数大小,matlab会根据用户所输入的矩阵结构自动配置,并在此后的运算中按正确的数学法则自动的调整矩阵的维数。o数组是由一组实数、复数或者字符等排成的长方阵列(array),可以是“一维”、“二维”、“三维”或者“多维”。o数组运算:无论在数组上施加什么运算,对数组每个元素都平等地实施同样的操作(点运算符)。o数组使程序简单易读;指令更接近于数学计算公式;提高程序向量化程度;提高计算效率。Matlab及其应用3数组分类o按照

2、数组中的数据类型:n数值数组n字符串数组n元胞数组n结构数组o以数组的结构分:n一维n二维n三维n多维Matlab及其应用4实例o绘制函数 在 时的曲线。x=0:0.1:1;y=x.*exp(-x);plot(x,y)xlabel(x);ylabel(y);title=(y=x*exp(-x);x为一维数组涉及数组的运算为“点运算”Matlab及其应用54.1 数值数组o4.1.1 一维数组(narr1.m)n创建o逐个元素输入法(元素不多的数组)o冒号生成法(生成等步长一维数组)x=a:inc:b(inc默认为1,(b-a)为inc的整数倍,也可以不是)o定数线性(对数)采样法linspac

3、e和logspacen寻访和赋值(narr1.m)o对下标index的操作oMatlab下标从1开始,到end结束Matlab及其应用6%This segment demonstrate the creation of 1-D numerical arraya=2 pi/2 sqrt(3) 3+5ib=0:2:20c=2:12d=0:1.5:10e=linspace(1.1,9.6,12)f=logspace(2,4,11)%This segment demonstrate the access of 1-D numerical arrayx1=a(3)x2=a(1)/b(3)x3=c(2 4

4、 6)x4=d(2:6)x5=e(10:-2:1)x6=e(find(e5)x7=f(1 3 5 7 8 6 4 2)%This segment demonstrate the assignment of 1-D numerical arrayx3(2)=0x4(1 3 5)=0 0 0x4(2 4)=0Matlab及其应用7o4.1.2 二维数组(narr2.m、narr3.m)n创建o直接输入法(以行为单位,行间以;或者enter间隔,行内以空格或者,间隔)o以M文件创建n标识o全下标o单下标(按行显、按列存),不推荐“逻辑1”标识(确切说不是标识,而是逻辑数组应用)n寻访与赋值Matla

5、b及其应用8%-Creation of the 2-D array-a=3;b=5;c=1 a a+b*i b;pi sin(pi) b*i id=c;e=c(:,1:2)+i*d(1:2,:)narr3; %call script M-file am%-Access of the 2-D array-f=am(2,3)g=am(2:4:16) %按行显,按列存h=am(:,2)k=am(:)%-Assignment of the 2-D array-am(3,2)=0am(2 8 12)=0 0 0am(:)=0%-This is the scripts to create the 2-D

6、array-am=1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16;Matlab及其应用9Matlab及其应用10o4.1.3 数组运算n数组运算规则:无论在数组上施加什么运算,对数组每个元素都平等地实施同样的操作。n数组运算常用函数:(三角函数、指数对数函数、复数函数、圆整求余函数等)nMATLAB Functions: Functions - By Category: Elementary Matho4.1.4 矩阵运算n与数组运算的区别:二维数组是数的存储形式,矩阵是一种变换或者映射算子;二者数据结构(计算机表达)一致。n运算指令对比,见下页(Programm

7、ing and Data Types: M-File Programming: Operators)n数组乘除、次方、开方、指数运算等均为“点”运算符n请课外复习矩阵运算,注意区分矩阵左除与右除Matlab及其应用11数组运算与矩阵运算是完全不同的概念,数组运算是MATLAB特有的便于计算和表示的算法,而矩阵运算是有严格的数学定义的。 数组运算 矩阵运算 指令 含义 指令 含义 A. 非共轭转置 A 共轭转置 A.n A元素的n次幂 An 矩阵自乘n次 pAp为底A元素为幂 pA 标量的A乘方 A./BA,B对应元素除 A/B A右除B B.A 与上面等价 BAA左除B,于上不同exp(A)e

8、为底A元素为指数expm(A) A的矩阵指数函数log(A)A的元素求对数logm(A) A的矩阵对数函数sqrt(A)A元素求平方根sqrtm(A) A的阵平方根函数 A#BA、B对应元素间的关系运算,#运算符。 ABA、B对应元素间的逻辑运算,逻辑符。Matlab及其应用12o4.1.5 数组生成函数(narr4.m)odiag对角方阵(数组)oeye单位方阵(数组)omagic魔方阵(数组)oones全一阵(数组)orand随机阵(数组)orandn正态随机阵(数组)ozeros全零阵(数组)Matlab及其应用13%-methods of diag-a=diag(2 3 4 5)b=d

9、iag(2 3,-1)x=diag(b)u=diag(b,-1)%-methods of eye-c=eye(3)d=eye(3,2)e=eye(size(a)%-methods of magic-f=magic(3)%-methods of ones-g=ones(3)h=ones(2,3)k=ones(size(a)%-methods of zeros-l=zeros(3)m=zeros(2,3)n=zeros(size(a)%-methods of rand-%The rand function generates arrays of random % numbers whose ele

10、ments are%uniformly distributed in the interval (0,1).o=rand(3)p=rand(3,4)q=rand(size(a)y = 10; z = 50;randnum = y + (z-y) * rand(5)%-methods of randn-%The randn function generates arrays of % random numbers whose elements are %normally distributed with mean 0, variance and % standard deviation are

11、1.r=randn(3)s=randn(3,4)t=randn(size(a)Matlab及其应用14o4.1.6数组扩展n赋值扩展n多次寻访扩展n合成扩展A=reshape(1:9,3,3)A(5,5)=21A(:,6)=12A=A(:,1:6,1:6)B=ones(2,12)AB=A;BC=zeros(5,2)AC=A,CMatlab及其应用154.1.7 数组操作函数(narr5.m) 指令 含义 cat把大小相同的数组,按某一方向迭加为高维数组 diag提取对角线元素,或生成对角阵。 flipud以数组水平中线翻转180 fliplr以数组垂直中线翻转180 kron按 Kroneck

12、er 乘法产生 “积” 数组 repmat按指定行或列铺放数组,以形成更大的数组 reshape在总元素不变的情况下,改变数组的大小 rot90逆时针旋转二维数组90 tril提取数组下三角部分,并生成下三角阵 triu提取数组上三角部分,并生成上角阵Matlab及其应用16a=-4:4b=reshape(a,3,3)c=b+i*bd=be=cf=flipud(b) %flip up and downg=fliplr(b) %flip left and righth=cat(1,f,g) %cat the f and g in the 1st dimension,Columnk=cat(2,f

13、,g) %cat the f and g in the 2nd dimension,Rowl=cat(3,f,g) %cat the f and g in the 3rd dimension%K = kron(X,Y) returns the Kronecker tensor product of X and Y%If X is 2-by-3, then kron(X,Y) is % X(1,1)*Y X(1,2)*Y X(1,3)*Y% X(2,1)*Y X(2,2)*Y X(2,3)*Y m=kron(d,g)n=kron(g,d)%B = repmat(A,m,n) creates a

14、large matrix B consisting of an %m-by-n tiling of copies of Ap=repmat(b,2,2)Matlab及其应用17o4.1.8 关系操作(narr6.m)n运算符:, = , = , =n关系操作的结果为逻辑数n逻辑数:True和False,用1和0表示n所有非零数都为逻辑真,零为逻辑假n逻辑数组:所有元素为1或0的数组可以看为逻辑数组n标量可与任何维数组比较,比较在标量与每个元素间进行,数组间比较必须同维Matlab及其应用18t=-2*pi:pi/10:2*pi;y=sin(t)./t; %dot divide array op

15、eration, view the creation of NaN at t=0z=(t=0) %view the creation of logical arraytt=t+z*eps; %eps is the machine zeroyy=sin(tt)./tt;subplot(1,2,1),plot(t,y,r),axis(-7,7,-0.5,1.2),xlabel(t),ylabel=(y),title(figure with flaw)subplot(1,2,2),plot(tt,yy,b),axis(-7,7,-0.5,1.2),xlabel(tt),ylabel=(yy),tit

16、le(normal figure )r=randn(3,6)logic=abs(r)1.5r(logic)=0s=(find(r=0)r(s)=11i,j=find(r=11);disp(i),disp(j)Matlab及其应用19o4.1.9 逻辑操作n操作符:& , | , (注意与C语言的区别)nnarr7.mn一些关系逻辑函数t=linspace(0,3*pi,500);y=sin(t); %生成正弦波z1=(t2*pi).*y; %对t在(0,pi)和(2*pi,3*pi)范围进行数组运算,其余为0w=(tpi/3 & t7*pi/3 & t8*pi/3); %取(pi/3,2*pi

17、/3)和(7*pi/3 , 8*pi/3)范围wn=w; %对w作 非 逻辑运算z2=w*sin(pi/3)+wn.*z1; %对正弦波削顶subplot(1,3,1),plot(t,y,r,LineWidth,3),ylabel(y)subplot(1,3,2),plot(t,z1,b,LineWidth,3),axis(0 10 -1 1)subplot(1,3,3),plot(t,z2,g,LineWidth,3),axis(0 10 -1 1)Matlab及其应用20函函 数数功功 能能all如果所有的元素都是非零如果所有的元素都是非零值,返回,返回1;否;否则,返回,返回0。any如

18、果有一个元素如果有一个元素为非零非零值,那么返回,那么返回1;否;否则,返回,返回0isempty判断是否空矩判断是否空矩阵isequal判断两矩判断两矩阵是否相同是否相同isreal判断是否是判断是否是实矩矩阵find返回一个由非零元素的下返回一个由非零元素的下标组成的向量成的向量isfinite判断各元素是否有限数判断各元素是否有限数isinf判断各元素是否无判断各元素是否无穷大大isletter判断各元素是否字符判断各元素是否字符isnan判断各元素是否判断各元素是否“非数非数”isprime判断各元素是否判断各元素是否质数数isspace判断各元素是否空格判断各元素是否空格Matlab

19、及其应用21o4.1.10 高维数组n创建o全下标赋值o低维数组合成o数组生成函数生成o数组函数构造(cat,repmat,reshape)n标识ondims获得数组的维数osize获得数组各维的大小olength获得数组的所有维中的最大维Matlab及其应用22o4.1.11 “非数”和“空”数组n非数:NaN,nann产生:0/0, / , 0 n传递:对NaN(nan)作运算,产生的还是NaNn判断:isnan函数n“空数组”:a=, b=ones(2,0)n判断:isempty函数空数组用途:A=reshape(-4:5,2,5)A(:,2,4)=Matlab及其应用234.2 字符串

20、数组o4.2.1 字符串入门na=123.45nb=123.45nc=examplen数值量占8字节,字符存放占2字节n必须在英文半角状态输入 Matlab及其应用24o4.2.2 字符串数组(narr8.m)n创建:a=Matlab programmingnsize函数:size(a)n标识:与数组访问方式一致nASCII码n当字符串本身包含时,用两个联系的单引号(不能用双引号)标识Matlab及其应用25a=123.45; %a is a numerical numberb=123.45; %b is a character arrayc=Matlab programming;whoscf

21、irst=c(1:6) %get the ahead 6 bits of ccinvert=c(end:-1:1) %invert the character of cc(1:6)= c/c+ %assignment of the first 6 bits of cd=and ;e=Matlab ,d,c(1:6),c(7:end) %combination into a new character arrayf=Marys score of ,c, is exelent %the in character is expressed by g=double(f) %convert charac

22、ter array into ASCII(integer)h=char(g) %convert the integer into character arrayMatlab及其应用26o4.2.3 复杂字符串数组(二维字符串数组)n创建(narr9.m)o直接输入(保证每行等长)o串操作函数char,str2mat,strvcato转换函数int2str,num2str,mat2strMatlab及其应用27clear a=2;w=3;t=0:0.01:10;y=exp(-a*t).*sin(w*t); %calculate the functionymax,imax=max(y); %get

23、 the maximum value and index of yttext=t=,num2str(t(imax); %function of num2strytext=y=,num2str(ymax);maxtext=char(maximum:, ttext,ytext) %char transfer into a character arraytit=y=exp(-,num2str(a),t)*sin(,num2str(w),t);plot(t,zeros(size(t),k) %plot a base line at y=0hold onplot(t,y,b,LineWidth,2) %

24、plot the functionplot(t(imax),ymax,r.,MarkerSize,30) %plot the markertext(t(imax)+0.3,ymax+0.05,maxtext) %output the texttitle(tit),xlabel(t),ylabel(y),hold offMatlab及其应用28o4.2.4 字符串函数n串转换函数n串操作函数nMATLAB Functions: Functions - By Category: Data TypesMatlab及其应用294.3 元胞数组o4.3.1 元胞数组与其他数组的不同n“银行的保险箱”nC

25、ell array,元胞(cell)为基本单位n每个元胞中可以存放任何类型,任何大小的数据n用 创建oA(m,n),元胞外标识,表示寻访元胞oam,n,元胞内编址,表示寻访元胞内容n元胞数组显示celldispMatlab及其应用30o4.3.2 扩充、收缩与重组n创建函数cell:cell(3)、cell(2,3)n列扩充(空格分隔)、行扩充(;分隔)oa=cell(2); b=cell(2,3)o列扩充:c=a b或c=a,bo行扩充:c=a;b,由于行数不同出错ncellplot图形表示元胞数组内容n收缩:用空数组删除某行、某列n重组函数:reshapeMatlab及其应用31o4.3.

26、3 元胞数组访问n()访问取得一个元胞n取得一个元胞的内容cstr=char(元胞数组示例,第一个元胞(cell)为字符串); %define cstr as a character arrayrnum=reshape(1:9,3,3); %create a 3x3 numerical arrayiconst=1+2i; %iconst is a imaginary constsymfun=sym(sin(-3*t)*exp(-t); %define a symbolic functionCelary1=cstr rnum;iconst symfun %define the four diff

27、erent variables as a cell arrayCelary21,1=cstr; %another methods for the cell array definitionCelary21,2=iconst;Celary22,1=rnum;Celary22,2=symfun;Celary2 %display the cell arrayCelary3=Celary1 Celary2 %column expansion of cell arrayCelary4=Celary1;Celary2 %line expansion of cell arrayCelary3(2,:)= %

28、delete the line #2 of the cell arrayCelary3=reshape(Celary3,2,2) %reshape the cell arraycellplot(Celary4,legend) %plot the cell array as a figuretype=Celary4(3,1) %get the 3,1 of the cell arrayCelary43,1=12.36 %make the content of 3,1 in the cell array into 12.36item1 item2 item3=deal(Celary42 3 4)

29、%deal function; access cell array with , %item1,item2,item3=deal(Celary42,3,4) is the same as the above line Matlab及其应用32 4.4 结构数组o4.4.1 与元胞数组的区别n数据存放场所(结构数组存在域中)n访问方式:元胞用,结构用域名操作符“.”n结构数组的域必须为元胞数组o创建n直接赋值法n构造函数structs = struct(field1,field2,.)s = struct(field1,values1,field2,values2,.)o应用Matlab及其应用

30、33cleargh.name= No.1 house;gh.vol=2000 square meters;gh.para.temp=31.2 30.4 31.6 28.7;29.7 31.1 30.9 29.6;gh.para.humid=62.1 59.5 57.7 56.9;60.2 61.5 59.4 58.9;ghgh.paragh.para.temp% complete the definition of the structuregh1=struct(name,vol,para,);gh1(2,3).name=No.1 house;gh1(2,3).vol=2000 square

31、meters;gh1(2,3).para.temp=31.2 30.4 31.6 28.7;29.7 31.1 30.9 29.6;gh1(2,3).para.humid=62.1 59.5 57.7 56.9;60.2 61.5 59.4 58.9;gh1%another methods for structure array definitiongh(2,3).name=No.6 house; %creation of the structure arrayghgh(2,3)Matlab及其应用34nex=5;for k=1:nex ex(k).f=(k-1)*nex+1:5;endexdisp(blanks(8),content in structure)for k=1:nex disp(ex(k).f)endclass(ex(1).f)sumf=zeros(1,5);for k=1:nex sumf=sumf+ex(k).f;endsumfdisp(blanks(8),square root of the content in structure)for k=1:nex disp(sqrt(ex(k).f)endMatlab及其应用354.5 数据类型归纳Matlab及其应用36

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号