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

前言:

单元数组和结构数组是一种新的数据类型,能将不同类型、不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护。

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

一、单元数组(细胞数组)

 

在单元数组中,通过单元数组的名字是不能访问相应的元素,只能访问对应的索引号,因为单元数组中存储的是指向某种数据结构的指针。

创建并赋值:

1.赋值语句创建:分为内容创建和单元索引创建

内容创建:一个一个元素进行创建,用大括号

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

单元索引创建:一个一个单元进行创建,用小括号

c(,)={[ ; ]};
c(,)={[ ; ; ]};
c(,)={[]};
c(,)={'i love pig'};
b=c(,);
d=c{,};
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{,}=[ ; ];
c{,}=[ ; ; ];
c{,}=[];
c{,}='i love a pig';
cellplot(c)

结果如图:2*2的单元矩阵,红色表示占用内存,白色相反,字符串最后怎么没开辟内存?

二、结构数组

引入结构数组原因:普通数据和单元数组只能通过下标访问数组元素,而结构数组是元素带名字的,也可以存储不同类型的元素,元素被称为域,数组名.域名可以访问结构数组的具体元素值。

1.创建

赋值语句创建:

student().name='bob';
student().sex='man';
student().age='';
student().score=[ ];
student().name='Plimmer';
student().sex='man';
student().age='';
student().score=[ ];
student().name='liky';
student().sex='girl';
student().score=[ ];

比如:执行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()=struct('name','bob','sex','man','age',,'score',[  ]);
student()=struct('name','Plimmer','sex','man','age',,'score',[ ]);
student()=struct('name','liky','sex','girl','age','','score',[ ]);
%operate:
>> student().name%访问数组名student()的域名name
ans =
Plimmer
>>student().hobby='music'%增加域名hobby
student =
1x3 struct array with fields:
name
sex
age
score
hobby
>> student()%访问数组名student()
ans =
name: 'bob'
sex: 'man'
age:
score: [ ]
hobby: []

用rmfield()函数去删除结构数组里的域名。
s = rmfield(s,field) removes the specified field or fields from structure array s.

>> student()=struct('name','bob','sex','man','age',,'score',[  ]);
student()=struct('name','Plimmer','sex','man','age',,'score',[ ]);
student()=struct('name','liky','sex','girl','age','','score',[ ]);
%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

注:还有好多函数对结构数组进行操作,太多了,不写上面了碰到再说吧

最新文章

  1. CSS3回执特殊图形
  2. javascript里面foreach遍历函数介绍,以及跟jquery里面each的区别
  3. 使用multipart请求处理文件上传
  4. V$SGA_RESIZE_OPS.STATUS = ERROR, and MMAN / auto-tuning stops.
  5. 数据库实例创建好后,用plsql登录居然提示ora-12526监听程序:所有适用例程都处于受限模式的问题
  6. Alljoyn 概述(3)
  7. OpenRisc-50-or1200的freeze模块分析
  8. poj3468(线段树)
  9. Python垃圾回收机制 总结
  10. 温故而知新----stack
  11. OC调用c++函数
  12. c++利用类进行单链表的插入,删除,清空操作
  13. bind()方法
  14. [WC2014]紫荆花之恋
  15. AlphaRacks 2018年黑五 VPS $3.99/年
  16. Oracle 12cR1中性能优化新特性之全数据库缓冲模式
  17. DataGridView 获取当前单元格
  18. winfrom 实现窗体圆角
  19. UNIX网络编程读书笔记:基本TCP套接口编程
  20. wifi的web 认证。

热门文章

  1. Linux 对比 Windows 缺点
  2. JAVA程序员常用软件整理
  3. [原创] [C#] 转换Excel数字列号为字母列号
  4. [RK3288][Android6.0] 关于uboot中logo相关知识点小结【转】
  5. 谈一谈以太坊虚拟机EVM的缺陷与不足
  6. Ubuntu redmine 安装
  7. Python sklearn Adaboost
  8. MySQL之不得不说的keepsync和trysync
  9. FOJ2252:Yu-Gi-Oh!
  10. bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】