Matlab基础之单元数组和结构数组

Matlab基础之单元数组和结构数组 前言:
单元数组和结构数组是一种新的数据类型,能将不同类型、不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护。
Matlab基础之单元数组和结构数组
文章图片

如上图所示的2*2矩阵中,分别存储着四种不同的数据类型,分别为数组、字符串、空矩阵、复数矩阵。

一、单元数组(细胞数组) 在单元数组中,通过单元数组的名字是不能访问相应的元素,只能访问对应的索引号,因为单元数组中存储的是指向某种数据结构的指针。
创建并赋值:
【Matlab基础之单元数组和结构数组】1.赋值语句创建:分为内容创建和单元索引创建
内容创建:一个一个元素进行创建,用大括号

c{1,1}=[1 2; 3 4]; c{1,2}=[1 2; 3 4; 2 14]; c{2,1}=[]; c{2,2}='i love a pig'; b=c(2,2); d=c{2,2}; c b d %%%%%% result: c = [2x2 double][3x2 double] []'i love a pig' b = 'i love a pig'd = i love a pig

单元索引创建:一个一个单元进行创建,用小括号
c(1,1)={[1 2; 3 4]}; c(1,2)={[1 2; 3 4; 2 14]}; c(2,1)={[]}; c(2,2)={'i love pig'}; b=c(2,2); d=c{2,2}; c b d %%%%%% result: c = [2x2 double][3x2 double] []'i love a pig' b = 'i love a pig'd = i love a pig

注意:单元矩阵与普通矩阵名字不能相同,否则偶同矩阵覆盖单元矩阵。



2.cell()函数创建:
>> b=cell(2,3)
b =
[][][]
[][][]
对它赋值如上面的方法,分内容和单元创建两种方法。
3.用大括号直接创建并赋值:
如3*4的单元矩阵
>> b={[2 3; 4 6],'you are a pig',[],[2; 2; 1]; [2 3; 4 6],'you are a pig',[],[2; 2; 1]; [2 3; 4 6],'you are a pig',[],[2; 2; 1]}
b =
[2x2 double]'you are a pig'[][3x1 double]
[2x2 double]'you are a pig'[][3x1 double]
[2x2 double]'you are a pig'[][3x1 double]
总结:第三种创建方法最简单和方便!
4.如何显示
上面的方法也介绍如何显示单元数组,但只能显示其中一个元素。
1)用celldisp()函数能全部整体显示单元数组的细节内容。
2)用cellplot()函数以图形方式展现:
c{1,1}=[1 2; 3 4]; c{1,2}=[1 2; 3 4; 2 14]; c{2,1}=[]; c{2,2}='i love a pig'; cellplot(c)

结果如图:2*2的单元矩阵,红色表示占用内存,白色相反,字符串最后怎么没开辟内存?
Matlab基础之单元数组和结构数组
文章图片

二、结构数组

引入结构数组原因:普通数据和单元数组只能通过下标访问数组元素,而结构数组是元素带名字的,也可以存储不同类型的元素,元素被称为域,数组名.域名可以访问结构数组的具体元素值。
1.创建
赋值语句创建:
student(1).name='bob'; student(1).sex='man'; student(1).age='25'; student(1).score=[98 99 100]; student(2).name='Plimmer'; student(2).sex='man'; student(2).age='12'; student(2).score=[98 9 100]; student(3).name='liky'; student(3).sex='girl'; student(3).score=[98 99 97];

比如:执行student(2).age返回 ans =12;

执行student(3).age返回 ans=[];
执行student(2)返回
ans =
name: 'Plimmer'
sex: 'man'
age: '12'
score: [98 9 100]
struct()函数创建:
帮助文档的定义:s = struct(field1,value1,...,fieldN,valueN)=sstruct(域名,值,域名,值,域名,值,。。。。),上面的用struct()来实现:
>> student(1)=struct('name','bob','sex','man','age',25,'score',[98 99 100]); student(2)=struct('name','Plimmer','sex','man','age',12,'score',[98 9 100]); student(3)=struct('name','liky','sex','girl','age','','score',[98 99 97]); %operate: >> student(2).name%访问数组名student(2)的域名name ans = Plimmer >>student(2).hobby='music'%增加域名hobby student = 1x3 struct array with fields: name sex age score hobby >> student(1)%访问数组名student(1) ans = name: 'bob' sex: 'man' age: 25 score: [98 99 100] hobby: []

用rmfield()函数去删除结构数组里的域名。
s = rmfield(s,field) removes the specified field or fields from structure array s.
>> student(1)=struct('name','bob','sex','man','age',25,'score',[98 99 100]); student(2)=struct('name','Plimmer','sex','man','age',12,'score',[98 9 100]); student(3)=struct('name','liky','sex','girl','age','','score',[98 99 97]); %operate: >> student=rmfield(student,'age')%一次只能删除一个域名 student = 1x3 struct array with fields: name sex score>> student%验证 student = 1x3 struct array with fields: name sex score>> fields={'age','sex','score'}; %一次能删除多个域名 student= rmfield(student,fields) student = 1x3 struct array with fields: name >> student%验证 student = 1x3 struct array with fields: name

注:还有好多函数对结构数组进行操作,太多了,不写上面了碰到再说吧
转载于:https://www.cnblogs.com/jiahenhe2/p/7814485.html

    推荐阅读