Matlab基礎之單元數組和結構數組


Matlab基礎之單元數組和結構數組

前言:

單元數組和結構數組是一種新的數據類型,能將不同類型、不同維數的數組組合在一起,從而方便對不同的數據類型方便管理和維護。

如上圖所示的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

注:還有好多函數對結構數組進行操作,太多了,不寫上面了碰到再說吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM