Matlab經(jīng)典教程 (2).doc_第1頁
Matlab經(jīng)典教程 (2).doc_第2頁
Matlab經(jīng)典教程 (2).doc_第3頁
Matlab經(jīng)典教程 (2).doc_第4頁
Matlab經(jīng)典教程 (2).doc_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第三章 字符串、元胞和構(gòu)架數(shù)組MATLAB 6.x版的內(nèi)建數(shù)據(jù)類型(Built-in data type)就有5種以上,此外還有許多其他專門設(shè)計(jì)的類(Class),如符號類、內(nèi)聯(lián)函數(shù)類、控制工具包中的線性時(shí)不變模型類、神經(jīng)網(wǎng)絡(luò)類等。就程序設(shè)計(jì)而言,MATLAB 6.x版采用了面向?qū)ο缶幊碳夹g(shù)。數(shù)據(jù)和編程的改變使用戶能更簡捷而自然地解決復(fù)雜的計(jì)算問題(如符號計(jì)算問題、多變量控制系統(tǒng)問題、神經(jīng)網(wǎng)絡(luò)問題)。本章內(nèi)容根據(jù)MATLAB6.5編寫,但絕大部分內(nèi)容適用于其他MATLAB6.x版本。第二章介紹了數(shù)值數(shù)組(Numeric Array),這是讀者比較熟悉的數(shù)據(jù)類型。本章將集中講述另外三類數(shù)據(jù):字符串?dāng)?shù)組(Character String Array)、元胞數(shù)組(Cell array)和構(gòu)架數(shù)組(Structure array)。它們之間的基本差別見表3-1。表3-1 四種數(shù)據(jù)類型基本構(gòu)成比較表數(shù)組類型基本組分組分內(nèi)涵基本組分占用字節(jié)數(shù)數(shù)值數(shù)組元素雙精度實(shí)數(shù)標(biāo)量或雙精度復(fù)數(shù)標(biāo)量816字符串?dāng)?shù)組元素字符2元胞數(shù)組元胞可以存放任何類型、任何大小的數(shù)據(jù)。不定構(gòu)架數(shù)組構(gòu)架只有掛接在構(gòu)架上的“域”才能存放數(shù)據(jù)。數(shù)據(jù)可以是任何類型、任何大小。不定3.1 字符串?dāng)?shù)組3.1.1 字符串入門【例3.1.1-1】先請讀者實(shí)際操作本例,以體會(huì)數(shù)值量與字符串的區(qū)別。cleara=12345.6789class(a) a_s=size(a) a = 1.2346e+004ans =doublea_s = 1 1 b=S class(b) b_s=size(b) b =Sans =charb_s = 1 1 whos Name Size Bytes Class a 1x1 8 double array a_s 1x2 16 double array ans 1x4 8 char array b 1x1 2 char array b_s 1x2 16 double arrayGrand total is 10 elements using 50 bytes 3.1.2 串?dāng)?shù)組的屬性和標(biāo)識【例3.1.2-1】本例演示:串的基本屬性、標(biāo)識和簡單操作。(1)a=This is an example. a =This is an example. (2)size(a) ans = 1 19 (3)a14=a(1:4) ra=a(end:-1:1) a14 =Thisra =.elpmaxe na si sihT (4)ascii_a=double(a) ascii_a = Columns 1 through 12 84 104 105 115 32 105 115 32 97 110 32 101 Columns 13 through 19 120 97 109 112 108 101 46 char(ascii_a) ans =This is an example. (5)w=find(a=a&a=z);ascii_a(w)=ascii_a(w)-32;char(ascii_a) ans =THIS IS AN EXAMPLE. (6)A=這是一個(gè)算例。; A_s=size(A) A56=A(5 6) ASCII_A=double(A) A_s = 1 7A56 =算例ASCII_A = Columns 1 through 6 54754 51911 53947 47350 52195 49405 Column 7 41379 char(ASCII_A) ans =這是一個(gè)算例。 (7)b=Example 3.1.2-1 b =Example 3.1.2-1 (8)ab=a(1:7), ,b, . ab =This is Example 3.1.2-1 . 3.1.3 復(fù)雜串?dāng)?shù)組的創(chuàng)建 多行串?dāng)?shù)組的直接創(chuàng)建【例-1】多行串?dāng)?shù)組的直接輸入示例。clearS=This string array has multiple rows. S =This string array has multiple rows. size(S) ans = 2 18 利用串操作函數(shù)創(chuàng)建多行串?dāng)?shù)組【例-1】演示:用專門函數(shù)char , str2mat , strvcat創(chuàng)建多行串?dāng)?shù)組示例。S1=char(This string array,has two rows.) S1 =This string arrayhas two rows. S2=str2mat(這,字符,串?dāng)?shù)組,由4行組成) S2 =這 字符 串?dāng)?shù)組 由4行組成 S3=strvcat(這,字符,串?dāng)?shù)組,由4行組成) S3 =這 字符 串?dāng)?shù)組 由4行組成 size(S3) ans = 5 5 轉(zhuǎn)換函數(shù)產(chǎn)生數(shù)碼字符串【例-1】最常用的數(shù)組/字符串轉(zhuǎn)換函數(shù)int2str , num2str , mat2str 示例。(1)A=eye(2,4); A_str1=int2str(A) A_str1 =1 0 0 00 1 0 0 (2)rand(state,0)B=rand(2,4); B3=num2str(B,3) B3 = 0.95 0.607 0.891 0.4560.231 0.486 0.762 0.0185 (3)B_str=mat2str(B,4) B_str =0.9501 0.6068 0.8913 0.4565;0.2311 0.486 0.7621 0.0185 Expression=exp(-,B_str,);eval(Expression) ans = 0.3867 0.5451 0.4101 0.6335 0.7937 0.6151 0.4667 0.9817 【例-2】綜合例題:在MATLAB計(jì)算生成的圖形上標(biāo)出圖名和最大值點(diǎn)坐標(biāo)。cleara=2;w=3;t=0:0.01:10;y=exp(-a*t).*sin(w*t);y_max,i_max=max(y);t_text=t=,num2str(t(i_max);%y_text=y=,num2str(y_max);%max_text=char(maximum,t_text,y_text);%tit=y=exp(-,num2str(a),t)*sin(,num2str(w),t);%plot(t,zeros(size(t),k)hold onplot(t,y,b)plot(t(i_max),y_max,r.,MarkerSize,20)text(t(i_max)+0.3,y_max+0.05,max_text)%title(tit),xlabel(t),ylabel(y),hold off 圖 3.1-1 利用元胞數(shù)組創(chuàng)建復(fù)雜字符串【例-1】元胞數(shù)組在存放和操作字符串上的應(yīng)用。a=MATLAB 6.x ;b=includes new data types:;c1=Multidimensional array;c2=User-definable data structure;c3=Cell arrays;c4=Character array;c5=Function handle;c=char(c1,c2,c3,c4,c5);C=a;b;c;%disp(C1:2)%disp( )%disp(C3)% MATLAB 6.x includes new data types: Multidimensional array User-definable data structureCell arrays Character array Function handle 3.1.4 串轉(zhuǎn)換函數(shù)【例3.1.4-1】fprintf, sprintf, sscanf的用法示例。rand(state,0);a=rand(2,2);s1=num2str(a)s_s=sprintf(%.10en,a) s1 =0.95013 0.606840.23114 0.48598s_s =9.5012928515e-0012.3113851357e-0016.0684258354e-0014.8598246871e-001 fprintf(%.5g,a) 0.950130.231140.606840.48598 s_sscan=sscanf(s_s,%f,3,2) s_sscan = 0.9501 0.4860 0.2311 0 0.6068 0 3.1.5 串操作函數(shù)3.2 元胞數(shù)組3.2.1 元胞數(shù)組的創(chuàng)建和顯示 元胞標(biāo)識尋訪和內(nèi)容編址尋訪的不同 元胞數(shù)組的創(chuàng)建和顯示【例 -1】本例演示:元胞數(shù)組的創(chuàng)建。C_str=char(這是,元胞數(shù)組創(chuàng)建算例 1);R=reshape(1:9,3,3);Cn=1+2i;S_sym=sym(sin(-3*t)*exp(-t); (1)直接創(chuàng)建法之一A(1,1)=C_str;A(1,2)=R;A(2,1)=Cn;A(2,2)=S_sym;A A = 2x10 char 3x3 double 1.0000+ 2.0000i 1x1 sym (2)直接創(chuàng)建法之二B1,1=C_str;B1,2=R;B2,1=Cn;B2,2=S_sym;celldisp(B) B1,1 =這是 元胞數(shù)組創(chuàng)建算例 1B2,1 = 1.0000 + 2.0000iB1,2 = 1 4 7 2 5 8 3 6 9B2,2 =sin(-3*t)*exp(-t) 3.2.2 元胞數(shù)組的擴(kuò)充、收縮和重組【例3.2.2-1】元胞數(shù)組的擴(kuò)充。(1)C=cell(2); C(:,1)=char(Another,text string);10:-1:1 C = 2x11 char 1x10 double (2)AC=A C A_C=A;C AC = 2x10 char 3x3 double 2x11 char 1.0000+ 2.0000i 1x1 sym 1x10 double A_C = 2x10 char 3x3 double 1.0000+ 2.0000i 1x1 sym 2x11 char 1x10 double 【例3.2.2-2】cellplot能用圖形形象化地表示元胞數(shù)組的內(nèi)容。(A_C取自上例)cellplot(A_C,legend) 圖 3.2-1 【例3.2.2-3】元胞數(shù)組的收縮和重組。(1)A_C(3,:)= A_C = 2x10 char 3x3 double 1.0000+ 2.0000i 1x1 sym 1x10 double (2)R_A_C=reshape(A_C,2,3) R_A_C = 2x10 char 1x10 double 1x1 sym1.0000+ 2.0000i 3x3 double 3.2.3 元胞數(shù)組內(nèi)容的調(diào)取【例3.2.3-1】元胞數(shù)組內(nèi)容的調(diào)取示例。(1)f1=R_A_C(1,3) class(f1) f1 = 1x1 symans =cell (2)f2=R_A_C1,3 class(f2) f2 =sin(-3*t)*exp(-t)ans =sym (3)f3=R_A_C1,1(:,1 2 5 6) f3 =這是 元胞創(chuàng)建 (4)f4,f5,f6=deal(R_A_C1,3,4) f4 =這是 元胞數(shù)組創(chuàng)建算例 1f5 = 10 9 8 7 6 5 4 3 2 1f6 = 1 4 7 2 5 8 3 6 9 3.2.4 元胞數(shù)組轉(zhuǎn)換函數(shù)【例3.2.4-1】常用元胞數(shù)組轉(zhuǎn)換函示例。(1)num2cell 把數(shù)值數(shù)組轉(zhuǎn)換成元胞數(shù)組rand(state,0);A=rand(2,3,2) C1=num2cell(A) A(:,:,1) = 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621A(:,:,2) = 0.4565 0.8214 0.6154 0.0185 0.4447 0.7919C1(:,:,1) = 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621C1(:,:,2) = 0.4565 0.8214 0.6154 0.0185 0.4447 0.7919 C2=num2cell(A,1) C2(:,:,1) = 2x1 double 2x1 double 2x1 doubleC2(:,:,2) = 2x1 double 2x1 double 2x1 double C3=num2cell(A,2,3) C3 = 1x3x2 double 1x3x2 double (2) clear,x=zeros(4,5);x(:)=1:20 C4=mat2cell(x, 2 2, 3 2)celldisp(C4) x = 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20C4 = 2x3 double 2x2 double 2x3 double 2x2 doubleC41,1 = 1 5 9 2 6 10C42,1 = 3 7 11 4 8 12C41,2 = 13 17 14 18C42,2 = 15 19 16 20 (3)D=cell2mat(C4(1,:) D = 1 5 9 13 17 2 6 10 14 18 3.3 構(gòu)架數(shù)組3.3.1 構(gòu)架數(shù)組的創(chuàng)建和顯示 直接創(chuàng)建法及顯示【例-1】本例通過溫室數(shù)據(jù)(包括溫室名、容積、溫度、濕度等)演示:單構(gòu)架的創(chuàng)建和顯示。(1)green_=一號房;% green_house.volume=2000立方米;%green_house.parameter.temperature=31.2 30.4 31.6 28.729.7 31.1 30.9 29.6;%green_house.parameter.humidity=62.1 59.5 57.7 61.5 62.0 61.9 59.2 57.5;% (2)顯示“單構(gòu)架”結(jié)構(gòu)和內(nèi)容green_house% green_house = name: 一號房 volume: 2000立方米 parameter: 1x1 struct green_house.parameter% ans = temperature: 2x4 double humidity: 2x4 double green_house.parameter.temperature% ans = 31.2000 30.4000 31.6000 28.7000 29.7000 31.1000 30.9000 29.6000 【例-2】本例演示構(gòu)架數(shù)組的創(chuàng)建和顯示,并利用構(gòu)架數(shù)組保存一個(gè)溫室群的數(shù)據(jù)。本例的運(yùn)行以例-1為先導(dǎo)。(1)green_house(2,3).name=六號房;%(2)green_house% green_house = 2x3 struct array with fields: name volume parameter green_house(2,3)% ans = name: 六號房 volume: parameter: 利用構(gòu)造函數(shù)創(chuàng)建構(gòu)架數(shù)組【例-1】利用構(gòu)造函數(shù)struct,建立溫室群的數(shù)據(jù)庫。(1)a=cell(2,3);green_house_1=struct(name,a,volume,a,parameter,a(1,2)% green_house_1 = 2x3 struct array with fields: name volume parameter (2)green_house_2=struct(name,a,volume,parameter,)% green_house_2 = 2x3 struct array with fields: name volume parameter (3)green_hopuse_3(2,3)=struct(name,volume,parameter,)% green_hopuse_3 = 2x3 struct array with fields: name volume parameter (4)a1=六號房;a2=3200立方米;green_house_4(2,3)=struct(name,a1,volume,a2,parameter,);%T6=31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6;% green_house_4(2,3).parameter.temperature=T6;% green_house_4 green_house_4 = 2x3 struct array with fields: name volume parameter 3.3.2 構(gòu)架數(shù)組域中內(nèi)容的調(diào)取和設(shè)置【例3.3.2-1】本例目的:一,演示函數(shù)fieldnames , getfield , setfield的使用方法;二,讓讀者感受到構(gòu)架數(shù)組對應(yīng)用工具包的影響;三,演示struct函數(shù)把“對象”轉(zhuǎn)換為構(gòu)架的應(yīng)用。本例為獲得一個(gè)演練的構(gòu)架,借助Toolbox control 工具包中的tf函數(shù),先產(chǎn)生一個(gè)用傳遞函數(shù)描寫的LTI線性時(shí)不變2輸入2輸出系統(tǒng)。(1)Stf=tf(3,2;4 1,1,1 3 2,1 1 1;1 2 2 1,1 0) Transfer function from input 1 to output. 3 #1: - s2 + 3 s + 2 4 s + 1 #2: - s3 + 2 s2 + 2 s + 1 Transfer function from input 2 to output. 2 #1: - s2 + s + 1 1 #2: - s (2)SSTF=struct(Stf) SSTF = num: 2x2 cell den: 2x2 cell Variable: s lti: 1x1 lti (3)FN=fieldnames(SSTF)class(FN) FN = num den Variable ltians =cell (4)FC=getfield(SSTF,den,2,1)FC1poly2str(FC1,s) FC = 1x4 doubleans = 1 2 2 1ans = s3 + 2 s2 + 2 s + 1 (5)SSTF.num2,1SSTF=setfield(SSTF,num,2,1,1 3 1);SSTF.num2,1 ans = 0 0 4 1ans = 1 3 1 3.3.3 構(gòu)架數(shù)組操作深入 構(gòu)架數(shù)組的擴(kuò)充和收縮【例-1】本例演示構(gòu)架數(shù)組SSTF的擴(kuò)充和收縮。(本例以例3.3.2-1的運(yùn)行為基礎(chǔ)。)(1)size(SSTF) ans = 1 1 (2)SSTF(2,2)=struct(tf(1,1 1)size(SSTF) SSTF = 2x2 struct array with fields: num den Variable ltians = 2 2 (3)SSTF(1,:)=S22n=SSTF(1,2).num,S22d=SSTF(1,2).denprintsys(S22n1,S22d1) SSTF = 1x2 struct array with fields: num den Variable ltiS22n = 1x2 doubleS22d = 1x2 double num/den = 1 - s + 1 增添域和刪除域【例-1】對構(gòu)架數(shù)組green_house進(jìn)行域的增添和刪減操作。(1)clear,for k=1:10;department(k).number=No.,int2str(k);enddepartment department = 1x10 struct array with fields: number (2)department(1).teacher=40;department(1).student=300;department(1).PC_computer=40;department department = 1x10 struct array with fields: number teacher student PC_computer (3)department(2).teacher.male=35;department(2).teacher.female=13;D2T=department(2).teacher%第2構(gòu)架teacher域包含兩個(gè)子域D1T=department(1).teacher%第1構(gòu)架teacher域僅是一個(gè)數(shù) D2T = male: 35 female: 13D1T = 40 (4)department(2).teacher=rmfield(department(2).teacher,male);department(2).teacher ans = female: 13 (5)department=rmfield(department,student) department = 1x10 struct array with fields: number teacher PC_computer department=rmfield(department,teacher;PC_computer) department = 1x10 struct array with fields: number 數(shù)值運(yùn)算操作和函數(shù)對構(gòu)架數(shù)組的應(yīng)用【例-1】數(shù)值運(yùn)算操作和函數(shù)在構(gòu)架域上的作用。n_ex=5;for k=1:n_ex,ex(k).f=(k-1)*n_ex+1:5;end ex ex = 1x5 struct array with fields:f %disp(blanks(10) 構(gòu)架域中內(nèi)容)for k=1:n_ex,disp(ex(k).f),end 構(gòu)架域中內(nèi)容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class(ex(1).f) ans =double %sum_f=zeros(1,5);for k=1:n_ex,sum_f=sum_f+ex(k).f;end,sum_f sum_f = 55 60 65 70 75 %disp(blanks(20) ex.f的平方根值)for k=1:n_ex,disp(sqrt(ex(k).f),end ex.f的平方根值 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 3.0000 3.1623 3.3166 3.4641 3.6056 3.7417 3.8730 4.0000 4.1231 4.2426 4.3589 4.4721 4.5826 4.6904 4.7958 4.8990 5.0000 3.3.4 構(gòu)架數(shù)組和元胞數(shù)組之間的轉(zhuǎn)換【例3.3.4-1】指令struct2cell和cell2struct的使用。(1)for k=1:5,ex(k).s=No. int2str(k);ex(k).f=(k-1)*5+1:5;end (2)fprintf(%sn,ex.s域的內(nèi)容 );fprintf(%s,blanks(4)for k=1:5;fprintf(%s,ex(k).s blanks(1);endfprintf(%sn,blanks(1),fprintf(%sn,ex.f域的內(nèi)容 )for k=1:5;disp(ex(k).f);end ex.s域的內(nèi)容 No.1 No.2 No.3 No.4 No.5 ex.f域的內(nèi)容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論