版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
结构数组
创建
例3.26 下表为学生档案,数组名为student,试用结构数组表示
student(1).ID = 101;
student(2).ID = 102;
student(1).name = 'wang';
student(2).name = 'chang';
student(1).age = 21;
student(2).age = 20;
struct(student)
>> practice
ans =
包含以下字段的 1×2 struct 数组:
ID
name
age
结构数组与单元数组转换
S = struct('category', 'tree', 'heigh', '28.5', 'name', 'brich')
C = struct2cell(S)
size(C)
>> practice
S =
包含以下字段的 struct:
category: 'tree'
heigh: '28.5'
name: 'brich'
C =
3×1 cell 数组
{'tree' }
{'28.5' }
{'brich'}
ans =
3 1
例3.28 顺接例3.26,将结构数组student转换成单元数组C
student(1).ID = 101;
student(2).ID = 102;
student(1).name = 'wang';
student(2).name = 'chang';
student(1).age = 21;
student(2).age = 20;
struct(student)
C = struct2cell(student);
C = [C(:,1), C(:,2)]
>> practice
ans =
包含以下字段的 1×2 struct 数组:
ID
name
age
C =
3×2 cell 数组
{[ 101]} {[ 102]}
{'wang'} {'chang'}
{[ 21]} {[ 20]}
单元数组的数据处理
例3.29 求88,95,79,73的平均数
C{1,1} = 88;
C{1,2} = 95;
C{1,3} = 79;
C{1,4} = 73;
s = 0;
for i = 1 : 4
s(i) = C{1,i};
end, s, mean(s)
>> practice
s =
88 95 79 73
ans =
83.7500