数组及运算2课时.ppt

上传人:max****ui 文档编号:8584083 上传时间:2020-03-30 格式:PPT 页数:36 大小:475.50KB
返回 下载 相关 举报
数组及运算2课时.ppt_第1页
第1页 / 共36页
数组及运算2课时.ppt_第2页
第2页 / 共36页
数组及运算2课时.ppt_第3页
第3页 / 共36页
点击查看更多>>
资源描述
Matlab及其应用 1 Matlab及其应用 重庆大学光电工程学院 Matlab及其应用 2 MATLAB及应用 第四讲数组及其运算 Matlab及其应用 3 在matlab中 矩阵 变量 都不必事先定义维数大小 matlab会根据用户所输入的矩阵结构自动配置 并在此后的运算中按正确的数学法则自动的调整矩阵的维数 数组是由一组实数 复数或者字符等排成的长方阵列 array 可以是 一维 二维 三维 或者 多维 数组运算 无论在数组上施加什么运算 对数组每个元素都平等地实施同样的操作 点运算符 数组使程序简单易读 指令更接近于数学计算公式 提高程序向量化程度 提高计算效率 Matlab及其应用 4 数组分类 按照数组中的数据类型 数值数组字符串数组元胞数组结构数组以数组的结构分 一维二维三维多维 Matlab及其应用 5 实例 绘制函数在时的曲线 x 0 0 1 1 y x exp x plot x y xlabel x ylabel y title y x exp x x为一维数组涉及数组的运算为 点运算 Matlab及其应用 6 4 1数值数组 4 1 1一维数组 narr1 m 创建逐个元素输入法 元素不多的数组 冒号生成法 生成等步长一维数组 x a inc b inc默认为1 b a 为inc的整数倍 也可以不是 定数线性 对数 采样法linspace和logspace寻访和赋值 narr1 m 对下标index的操作Matlab下标从1开始 到end结束 Matlab及其应用 7 Thissegmentdemonstratethecreationof1 Dnumericalarraya 2pi 2sqrt 3 3 5i b 0 2 20c 2 12d 0 1 5 10e linspace 1 1 9 6 12 f logspace 2 4 11 Thissegmentdemonstratetheaccessof1 Dnumericalarrayx1 a 3 x2 a 1 b 3 x3 c 246 x4 d 2 6 x5 e 10 2 1 x6 e find e 5 x7 f 13578642 Thissegmentdemonstratetheassignmentof1 Dnumericalarrayx3 2 0 x4 135 000 x4 24 0 Matlab及其应用 8 4 1 2二维数组 narr2 m narr3 m 创建直接输入法 以行为单位 行间以 或者enter间隔 行内以空格或者 间隔 以M文件创建标识全下标单下标 按行显 按列存 不推荐 逻辑1 标识 确切说不是标识 而是逻辑数组应用 寻访与赋值 Matlab及其应用 9 Creationofthe2 Darray a 3 b 5 c 1aa b ib pisin pi b ii d c e c 1 2 i d 1 2 narr3 callscriptM fileam Accessofthe2 Darray f am 2 3 g am 2 4 16 按行显 按列存h am 2 k am Assignmentofthe2 Darray am 3 2 0am 2812 000 am 0 Thisisthescriptstocreatethe2 Darray am 1234 5678 9101112 13141516 Matlab及其应用 10 Matlab及其应用 11 4 1 3数组运算数组运算规则 无论在数组上施加什么运算 对数组每个元素都平等地实施同样的操作 数组运算常用函数 三角函数 指数对数函数 复数函数 圆整求余函数等 MATLABFunctions Functions ByCategory ElementaryMath4 1 4矩阵运算与数组运算的区别 二维数组是数的存储形式 矩阵是一种变换或者映射算子 二者数据结构 计算机表达 一致 运算指令对比 见下页 ProgrammingandDataTypes M FileProgramming Operators 数组乘除 次方 开方 指数运算等均为 点 运算符请课外复习矩阵运算 注意区分矩阵左除与右除 Matlab及其应用 12 数组运算与矩阵运算是完全不同的概念 数组运算是MATLAB特有的便于计算和表示的算法 而矩阵运算是有严格的数学定义的 Matlab及其应用 13 4 1 5数组生成函数 narr4 m diag对角方阵 数组 eye单位方阵 数组 magic魔方阵 数组 ones全一阵 数组 rand随机阵 数组 randn正态随机阵 数组 zeros全零阵 数组 Matlab及其应用 14 methodsofdiag a diag 2345 b diag 23 1 x diag b u diag b 1 methodsofeye c eye 3 d eye 3 2 e eye size a methodsofmagic f magic 3 methodsofones g ones 3 h ones 2 3 k ones size a methodsofzeros l zeros 3 m zeros 2 3 n zeros size a methodsofrand Therandfunctiongeneratesarraysofrandom numberswhoseelementsare uniformlydistributedintheinterval 0 1 o rand 3 p rand 3 4 q rand size a y 10 z 50 randnum y z y rand 5 methodsofrandn Therandnfunctiongeneratesarraysof randomnumberswhoseelementsare normallydistributedwithmean0 varianceand standarddeviationare1 r randn 3 s randn 3 4 t randn size a Matlab及其应用 15 4 1 6数组扩展赋值扩展多次寻访扩展合成扩展 A reshape 1 9 3 3 A 5 5 21A 6 12A A 1 6 1 6 B ones 2 12 AB A B C zeros 5 2 AC A C Matlab及其应用 16 4 1 7数组操作函数 narr5 m Matlab及其应用 17 a 4 4b reshape a 3 3 c b i bd b e c f flipud b flipupanddowng fliplr b flipleftandrighth cat 1 f g catthefandginthe1stdimension Columnk cat 2 f g catthefandginthe2nddimension Rowl cat 3 f g catthefandginthe3rddimension K kron X Y returnstheKroneckertensorproductofXandY IfXis2 by 3 thenkron X Y is X 1 1 YX 1 2 YX 1 3 Y X 2 1 YX 2 2 YX 2 3 Y m kron d g n kron g d B repmat A m n createsalargematrixBconsistingofan m by ntilingofcopiesofAp repmat b 2 2 Matlab及其应用 18 4 1 8关系操作 narr6 m 运算符 关系操作的结果为逻辑数逻辑数 True和False 用1和0表示所有非零数都为逻辑真 零为逻辑假逻辑数组 所有元素为1或0的数组可以看为逻辑数组标量可与任何维数组比较 比较在标量与每个元素间进行 数组间比较必须同维 Matlab及其应用 19 t 2 pi pi 10 2 pi y sin t t dotdividearrayoperation viewthecreationofNaNatt 0z t 0 viewthecreationoflogicalarraytt t z eps epsisthemachinezeroyy sin tt tt subplot 1 2 1 plot t y r axis 7 7 0 5 1 2 xlabel t ylabel y title figurewithflaw subplot 1 2 2 plot tt yy b axis 7 7 0 5 1 2 xlabel tt ylabel yy title normalfigure r randn 3 6 logic abs r 1 5r logic 0s find r 0 r s 11 i j find r 11 disp i disp j Matlab及其应用 20 4 1 9逻辑操作操作符 注意与C语言的区别 narr7 m一些关系逻辑函数 t linspace 0 3 pi 500 y sin t 生成正弦波z1 t2 pi y 对t在 0 pi 和 2 pi 3 pi 范围进行数组运算 其余为0w t pi 3 对正弦波削顶subplot 1 3 1 plot t y r LineWidth 3 ylabel y subplot 1 3 2 plot t z1 b LineWidth 3 axis 010 11 subplot 1 3 3 plot t z2 g LineWidth 3 axis 010 11 Matlab及其应用 21 Matlab及其应用 22 4 1 10高维数组创建全下标赋值低维数组合成数组生成函数生成数组函数构造 cat repmat reshape 标识ndims 获得数组的维数size 获得数组各维的大小length 获得数组的所有维中的最大维 Matlab及其应用 23 4 1 11 非数 和 空 数组非数 NaN nan产生 0 0 0 传递 对NaN nan 作运算 产生的还是NaN判断 isnan函数 空数组 a b ones 2 0 判断 isempty函数 空数组用途 A reshape 4 5 2 5 A 2 4 Matlab及其应用 24 4 2字符串数组 4 2 1字符串入门a 123 45b 123 45 c example 数值量占8字节 字符存放占2字节必须在英文半角状态输入 Matlab及其应用 25 4 2 2字符串数组 narr8 m 创建 a Matlabprogramming size函数 size a 标识 与数组访问方式一致ASCII码当字符串本身包含 时 用两个联系的单引号 不能用双引号 标识 Matlab及其应用 26 a 123 45 aisanumericalnumberb 123 45 bisacharacterarrayc Matlabprogramming whoscfirst c 1 6 gettheahead6bitsofccinvert c end 1 1 invertthecharacterofcc 1 6 c c assignmentofthefirst6bitsofcd and e Matlab d c 1 6 c 7 end combinationintoanewcharacterarrayf Mary sscoreof c is exelent the incharacterisexpressedby g double f convertcharacterarrayintoASCII integer h char g converttheintegerintocharacterarray Matlab及其应用 27 4 2 3复杂字符串数组 二维字符串数组 创建 narr9 m 直接输入 保证每行等长 串操作函数char str2mat strvcat转换函数int2str num2str mat2str Matlab及其应用 28 cleara 2 w 3 t 0 0 01 10 y exp a t sin w t calculatethefunction ymax imax max y getthemaximumvalueandindexofyttext t num2str t imax functionofnum2strytext y num2str ymax maxtext char maximum ttext ytext chartransferintoacharacterarraytit y exp num2str a t sin num2str w t plot t zeros size t k plotabaselineaty 0holdonplot t y b LineWidth 2 plotthefunctionplot t imax ymax r MarkerSize 30 plotthemarkertext t imax 0 3 ymax 0 05 maxtext outputthetexttitle tit xlabel t ylabel y holdoff Matlab及其应用 29 4 2 4字符串函数串转换函数串操作函数MATLABFunctions Functions ByCategory DataTypes Matlab及其应用 30 4 3元胞数组 4 3 1元胞数组与其他数组的不同 银行的保险箱 Cellarray 元胞 cell 为基本单位每个元胞中可以存放任何类型 任何大小的数据用 创建A m n 元胞外标识 表示寻访元胞a m n 元胞内编址 表示寻访元胞内容元胞数组显示celldisp Matlab及其应用 31 4 3 2扩充 收缩与重组创建函数cell cell 3 cell 2 3 列扩充 空格分隔 行扩充 分隔 a cell 2 b cell 2 3 列扩充 c ab 或c a b 行扩充 c a b 由于行数不同出错cellplot图形表示元胞数组内容收缩 用空数组删除某行 某列重组函数 reshape Matlab及其应用 32 4 3 3元胞数组访问 访问取得一个元胞 取得一个元胞的内容 cstr char 元胞数组示例 第一个元胞 cell 为字符串 definecstrasacharacterarrayrnum reshape 1 9 3 3 createa3x3numericalarrayiconst 1 2i iconstisaimaginaryconstsymfun sym sin 3 t exp t defineasymbolicfunctionCelary1 cstrrnum iconstsymfun definethefourdifferentvariablesasacellarrayCelary2 1 1 cstr anothermethodsforthecellarraydefinitionCelary2 1 2 iconst Celary2 2 1 rnum Celary2 2 2 symfun Celary2 displaythecellarrayCelary3 Celary1Celary2 columnexpansionofcellarrayCelary4 Celary1 Celary2 lineexpansionofcellarrayCelary3 2 deletetheline 2ofthecellarrayCelary3 reshape Celary3 2 2 reshapethecellarraycellplot Celary4 legend plotthecellarrayasafiguretype Celary4 3 1 getthe3 1ofthecellarrayCelary4 3 1 12 36 makethecontentof3 1inthecellarrayinto12 36 item1item2item3 deal Celary4 234 dealfunction accesscellarraywith item1 item2 item3 deal Celary4 2 3 4 isthesameastheaboveline Matlab及其应用 33 4 4结构数组 4 4 1与元胞数组的区别数据存放场所 结构数组存在域中 访问方式 元胞用 结构用域名操作符 结构数组的域必须为元胞数组创建直接赋值法构造函数structs struct field1 field2 s struct field1 values1 field2 values2 应用 Matlab及其应用 34 cleargh name No 1house gh vol 2000squaremeters gh para temp 31 230 431 628 7 29 731 130 929 6 gh para humid 62 159 557 756 9 60 261 559 458 9 ghgh paragh para temp completethedefinitionofthestructuregh1 struct name vol para gh1 2 3 name No 1house gh1 2 3 vol 2000squaremeters gh1 2 3 para temp 31 230 431 628 7 29 731 130 929 6 gh1 2 3 para humid 62 159 557 756 9 60 261 559 458 9 gh1 anothermethodsforstructurearraydefinitiongh 2 3 name No 6house creationofthestructurearrayghgh 2 3 Matlab及其应用 35 nex 5 fork 1 nexex k f k 1 nex 1 5 endexdisp blanks 8 contentinstructure fork 1 nexdisp ex k f endclass ex 1 f sumf zeros 1 5 fork 1 nexsumf sumf ex k f endsumfdisp blanks 8 squarerootofthecontentinstructure fork 1 nexdisp sqrt ex k f end Matlab及其应用 36 4 5数据类型归纳
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!