《matlab结构数组》ppt课件

上传人:tian****1990 文档编号:75829215 上传时间:2019-02-01 格式:PPT 页数:24 大小:303.81KB
返回 下载 相关 举报
《matlab结构数组》ppt课件_第1页
第1页 / 共24页
《matlab结构数组》ppt课件_第2页
第2页 / 共24页
《matlab结构数组》ppt课件_第3页
第3页 / 共24页
《matlab结构数组》ppt课件_第4页
第4页 / 共24页
《matlab结构数组》ppt课件_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《《matlab结构数组》ppt课件》由会员分享,可在线阅读,更多相关《《matlab结构数组》ppt课件(24页珍藏版)》请在金锄头文库上搜索。

1、MATLAB 程序设计与应用 结构数组,13-1结构数组的建立,每一个结构数组(Structure Array)可以包含很多个元素,每一个元素可以看成是一笔数据。因此每个元素可以包含数个字段(Fields),而每个字段可包含各个不同型态的数据。例如一个包含学生个人数据的结构数组,可能含有的字段是 name(学生姓名)、id(学号)、scores(小考成绩)等。要建立此种结构,可在指令列直接输入个字段的值。,结构数组之范例一,范例13-1 : struct01.m clear student %清除student变数 student.name = 洪鹏翔; % 加入 name 字段 studen

2、t.id = mr871912; % 加入 id 字段 student.scores = 58, 75, 62; % 加入 scores 字段 student % 秀出结果 student = name: 洪鹏翔 id: mr871912 scores: 58,75,62 此时student即代表一个结构数组的第一个元素,或是第一笔数据。,结构数组之范例二,范例13-2 : struct02.m clear student % 清除 student 变数 student.name = 洪鹏翔; % 加入 name 字段 student.id = mr871912; % 加入 id 字段 stu

3、dent.scores = 58, 75, 62; % 加入 scores 字段 % 以下是新加入的第二笔数据 student(2).name = 邱中人; student(2).id = mr872510; student(2).scores = 25, 36, 92; student %秀出结果 student = 1x2 struct array with fields: Name Id scores dent = 1x2 struct array with fields: Name Id scores,结构数组之范例二,此时student即代表一个12的结构数组。由于此结构数组已渐趋复

4、杂,MATLAB并不将所有字段值印出。欲显示某元素的特定字段值,可输入明确的叙述,例如student(2).scores等。 另一个建立结构数组的方法,则是使用struct指令,其格式如下: structureArray = struct(field1, value1, field2, value2,.) 其中field1、field2、是字段名称,value1、value2、则是字段所包含的数据。如果value1、value2、为异质数组(Cell Arrays,详见第上一章),则MATLAB为依序将异质数组的每个元素设定为每一个结构中相对应的字段值,如以下范例13-3:,结构数组之范例三,

5、范例13-3 : struct03.m student = struct(name, 张庭硕, 张庭安, scores, 50 60, 60 70); student(1) % 显示 student(1) student(2) % 显示 student(2) ans = name: 张庭硕 scores: 50 60 ans = name: 张庭安 scores: 60 70 在上述使用法中,张庭硕, 张庭安 和50 60, 60 70都是异质数组,因此他们的每个元素会被依次设定到每个结构之中。但是如果其中有一个异值数组的长度是1,那么MATLAB会进行纯量展开(Scalar Expansio

6、n)来自动补足,如以下范例13-4:,结构数组之范例四,范例13-4 : struct04.m student = struct(name, 张庭安,scores, 50 60, 90 100); student(1) % 显示 student(1) student(2) % 显示 student(2) ans = name: 张庭安 scores: 50 60 ans = name: 张庭安 scores: 90 100 在上述范例中,张庭安可视为异质数组的一个元素,因此在设定至student结构数组时,MATLAB会进行纯量展开,将张庭安分别设定到student的两个元素的name字段值。

7、,结构数组之范例五,结构数组可以是巢状式(Nested)的,也就是说,结构数组的字段可是另一个结构数组,我们可以藉此产生复杂的数据结构 范例13-5 : struct05.m student = struct(name, 张庭硕, 张庭安, scores, 50 60, 60 70); student(2).course(1).title = Web Programming; student(2).course(1).credits = 2; student(2).course(2).title = Numerical Method; student(2).course(2).credits

8、= 3; student(2).course ans = 1x2 struct array with fields: title credits,13-2取用及改变结构数组的数据,Student结构数组,Student(1),Student(2),Student(3),.name=banny .scores=85,80,92,78,.name=joey .scores=80,85,90,88,.name=batty .scores=88,82,90,80,范例13-6 : buildStruct01.m clear student % 清除 student 变数 student(1) = st

9、ruct(name, Banny, scores, 85,80,92,78); student(2) = struct(name, Joey, scores, 80,85,90,88); student(3) = struct(name, Betty, scores, 88,82,90,80); 上述的student结构数组,可图标如下:,Struct2cell指令,欲取用结构数组中所有元素内所有字段的数据,可用struct2cell指令,例如: values = struct2cell(student) values(:,:,1) = Banny 1x4 double values(:,:,

10、2) = Joey 1x4 double values(:,:,3) = Betty 1x4 double 请注意,传回的values是一个异质数组。一般而言,若输入struct2cell指令的结构变量维度为mn,且包含p个字段,则传回异质数组的维度为pmn。(在上例中,p = 2,m = 1, n = 3。),改变结构数组内容,在结构数组中,我们可以使用句点(”.”)来找出某一笔数据内的某一个特定字段值,例如我们仅想看看第二个学生是谁,此时我们可以输入: studentName = student(2).name studentName = Joey 在上例中,在此一学生结构数组的第二数组元

11、素student(2)之后加上一点,再接上姓名字段名称name,即可取用此学生的实际姓名资料Joey,本例中进一步再将取出的学生姓名Joey储存在使用者自设的变量studentName之后显示于屏幕上。 类似取用结构数组个别字段数据内容的作法,我们可以改变结构数组中个别字段的数据内容,例如: student(2).name = Alex; 在上例中,student(2)的姓名已由原先的Joey改变为Alex。,CAT指令,MATLAB提供了cat指令,以达到并排字段值的目的,其语法为: A = cat(dim, structureField) 其中,dim代表并排后所改变的维度。例如,欲将小考

12、成绩左右(水平)并排,可输入: cat(2, student.scores) % 2 代表左右并排以改变直行的维度 ans = 85 80 92 78 80 85 90 88 88 82 90 80 欲将小考成绩上下(垂直)并排,可输入: cat(1, student.scores) % 1 代表上下并排以改变横列的维度 ans = 85 80 92 78 80 85 90 88 88 82 90 80,计算平均,在进行并排时,必须确认被并排的字段值有相同的行数(上下并排)或列数(左右并排),否则就会因为维度不和而产生错误讯息。 若要计算每次考试(共四次)的平均分数,可输入: average1

13、 = mean(cat(1, student.scores) average1 = 84.3333 82.3333 90.6667 82.0000 若要计算每位学生(共三位)的平均分数,可输入下列表达式,但注意表达式后面单引号的使用代表矩阵的转置(Transpose),因为mean指令是对矩阵的每一直行进行平均值运算: average2 = mean(cat(1,student.scores) average2 = 83.7500 85.7500 85.0000,并排运算,由于并排运算常被用到,MATLAB又提供下列两种方法 方括号运算:可以左右合并结构数组中相同字段的数值矩阵,产生一个新的数

14、值矩阵。 大括号运算:可以左右合并结构数组中相同字段的数据,产生一个异值矩阵。 若要把scores字段值进行左右合并,可以输入如下:allScores = student.scores allScores = 85 80 92 78 80 85 90 88 88 82 90 80 若要把name字段值抽取出来,形成由字符串组成的异值数组,可以输入如下: allNames = student.name allNames = Banny Alex Betty,结构数组之范例七,范例13-7 : printStruct01.m clear student % 清除 student 变数 studen

15、t(1) = struct(name, 张庭硕, scores, 85, 80); student(2) = struct(name, 锺书蓉, scores, 80, 85); student(3) = struct(name, 黄念中, scores, 88, 82); for i = 1:length(student) % 打印出每个学生的名字 fprintf (student %g: %sn, i, student(i).name); end,student 1: 张庭硕 student 2: 锺书蓉 student 3: 黄念中,取得及改变字段数据,亦可用 getfield及setfield来取得及改变一个字段的数据 其指令使用格式如下: fieldValues = getfield (structureArray, arrayIndex, field, fieldIndex) newStructure = setfield (structureArray, arrayIndex, field,fieldIndex) 输入下列表达式即可取得第二位学生的第一次小考成绩: score

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

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