matlab_结构数组和单元数组

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

这里写图片描述

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

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

在单元数组中,通过单元数组的名字是不能访问相应的元素,只能访问对应的索引号,因为单元数组中存储的是指向某种数据结构的指针。
创建并赋值:
1.赋值语句创建: 内容创建单元索引创建

1.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 

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

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 = 
    []    []    []
    []    []    []

对它赋值如上面的方法,分内容和单元创建两种方法。

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.
[cpp] view plain copy
>> 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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值