Matlab基础之单元数组和结构数组
2016-10-03 19:25
676 查看
前言:
单元数组和结构数组是一种新的数据类型,能将不同类型、不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护。
如上图所示的2*2矩阵中,分别存储着四种不同的数据类型,分别为数组、字符串、空矩阵、复数矩阵。
创建并赋值:
1.赋值语句创建:分为内容创建和单元索引创建
内容创建:一个一个元素进行创建,用大括号
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()函数以图形方式展现:
结果如图:2*2的单元矩阵,红色表示占用内存,白色相反,字符串最后怎么没开辟内存?
1.创建
赋值语句创建:
执行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()来实现:
s = rmfield(s,field) removes the specified field or fields from structure array s.
注:还有好多函数对结构数组进行操作,太多了,不写上面了碰到再说吧
单元数组和结构数组是一种新的数据类型,能将不同类型、不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护。
如上图所示的2*2矩阵中,分别存储着四种不同的数据类型,分别为数组、字符串、空矩阵、复数矩阵。
一、单元数组(细胞数组)
在单元数组中,通过单元数组的名字是不能访问相应的元素,只能访问对应的索引号,因为单元数组中存储的是指向某种数据结构的指针。创建并赋值:
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的单元矩阵,红色表示占用内存,白色相反,字符串最后怎么没开辟内存?
二、结构数组
引入结构数组原因:普通数据和单元数组只能通过下标访问数组元素,而结构数组是元素带名字的,也可以存储不同类型的元素,元素被称为域,数组名.域名可以访问结构数组的具体元素值。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
注:还有好多函数对结构数组进行操作,太多了,不写上面了碰到再说吧
三、参考资料
http://pan.baidu.com/s/1hs3BeFa相关文章推荐
- matlab基本知识点3-单元数组与结构
- MATLAB函数句柄、单元数组和结构
- MATLAB中元胞数组的单元索引和内容索引
- 数据结构基础温故而知新(二)——数组
- [C基础] 二维结构体指针数组
- [C++学习历程]基础部分 C++中的指针数组和结构
- 05_java基础加强(数组、判断语句、循环结构、进制转换、常用类等)
- 05- 字符串、单元数组和结构
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记
- 南京三星面试准备3--数组&基础数据结构
- Java基础03:循环结构;函数;重载;数组
- [C++学习历程]基础部分 C++中的指针数组和结构
- 【C#基础知识】之结构、数组及常用的几种排序方法总结
- 数据结构基础_对一个数组进行升序排序
- [Matlab]基础教程学习笔记(二):程序控制结构
- C#学习笔记12:枚举、结构、数组基础学习
- C#基础-结构、枚举和数组(Day4)
- MATLAB 字符串 结构数据 单元数据
- Java之美[从菜鸟到高手演变]之数据结构基础、线性表、栈和队列、数组和字符串
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记