1 / 12

10.1 結構陣列 10.1.1 結構的使用

第十章 其它的資料型態. 10.1 結構陣列 10.1.1 結構的使用. Matlab 結構的概念源自 C 語言,其語法也和 C 語言的結構非常類似. 結構可將不同型態的資料組合成新的資料型態 結構可分成「結構名稱」與「欄位名稱」 要設定或是取用結構的欄位,可依循 「結構名稱 . 欄位名稱」. >> student.name='Tom'; >> student.id='u80579'; >> student.score=[77 69 88]; >> student student = name: 'Tom'

Download Presentation

10.1 結構陣列 10.1.1 結構的使用

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十章 其它的資料型態 10.1 結構陣列10.1.1 結構的使用 • Matlab結構的概念源自C語言,其語法也和C語言的結構非常類似 • 結構可將不同型態的資料組合成新的資料型態 • 結構可分成「結構名稱」與「欄位名稱」 • 要設定或是取用結構的欄位,可依循 「結構名稱.欄位名稱」

  2. >> student.name='Tom'; >> student.id='u80579'; >> student.score=[77 69 88]; >> student student = name: 'Tom' id: 'u80579' score: [77 69 88] • 如果想再定義另一個結構陣列的元素,可用下面的語法: >> student(2).name='Jerry'; >> student(2).id='u80161'; >> student(2).score=[89 78 90]; >> student student = 1x2 struct array with fields: name id score

  3. Matlab也提供了一個仿C語言的函數struct,可建立結構陣列: >> student(3)=struct('name','Tippi',… 'id','u80623','score',[86 77 95]) student = 1x3 struct array with fields: name id score

  4. 10.1.2 結構元素的擷取 • 要擷取結構陣列裡某個欄位的值,可依循 結構名稱(索引值) . 欄位名稱 >> student(2) ans = name: 'Jerry' id: 'u80161' score: [89 78 90] >> student(2).name ans = Jerry

  5. 10.1.3 編修結構陣列的欄位 • 下表列出了編修結構陣列的欄位之語法與相關函數: >> fieldnames(student) ans = 'name' 'id' 'score' >> student=rmfield(student,'age') student = 1x3 struct array with fields: name id score >> student(1).age=19 student = 1x3 struct array with fields: name id score age

  6. 10.2 多質陣列10.2.1 建立多質陣列 • 多質陣列(cell array)每一個元素的型態都可以不相同,這點有別於結構陣列。 • 要建立多質陣列,可用大括號,將多質陣列裡的所有元素括起來: >> A={'abc',1234,magic(3)} A = 'abc' [1234] [3x3 double] >> B={12,ones(3);magic(2),'str'} B = [ 12] [3x3 double] [2x2 double] 'str'

  7. 我們也可以依元素所在陣列之位置來建立元素,如下面的範例:我們也可以依元素所在陣列之位置來建立元素,如下面的範例: >> A2(1,1)={'abc'}; >> A2(1,2)={1234}; >> A2(1,3)={magic(3)}; >> A2 A2 = 'abc' [1234] [3x3 double] • 另一種設定多質陣列的方法是內容索引法 (content indexing): >> A3{1,1}='abc'; >> A3{1,2}=1234; >> A3{1,3}=magic(3); >> A3 A3 = 'abc' [1234] [3x3 double]

  8. 10.2.2 顯示多質陣列 • cellplot 函數可以用圖形來表示多質陣列 >> B={12,ones(3);magic(2),'str'}; >> cellplot(B)

  9. 10.2.3 多質陣列元素的擷取 • 多質陣列元素擷取的方式和一般的陣列大同小異: >> B={12,ones(3);magic(2),'str'} B = [ 12] [3x3 double] [2x2 double] 'str' >> B(1,2) ans = [3x3 double] >> B{1,2} ans = 1 1 1 1 1 1 1 1 1

  10. 多質陣列可以利用cell 函數預先配置記憶空間 >> F=cell(2,3) F = [] [] [] [] [] [] >> F{1,2}=magic(5) F = [] [5x5 double] [] [] [] []

  11. 10.3 多質陣列的轉換 • 下表列出了與多質陣列轉換相關的函數:

  12. >> A=magic(3) A = 8 1 6 3 5 7 4 9 2 >> num2cell(A) ans = [8] [1] [6] [3] [5] [7] [4] [9] [2] >> x=num2cell(A,1) x = [3x1 double] [3x1 double] [3x1 double] >> x{1} ans = 8 3 4

More Related