學(xué)習(xí)matlab程序-簡(jiǎn)單示例_第1頁(yè)
學(xué)習(xí)matlab程序-簡(jiǎn)單示例_第2頁(yè)
學(xué)習(xí)matlab程序-簡(jiǎn)單示例_第3頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、Matlab編程示例.程序結(jié)構(gòu)及函數(shù)作用在軟件Matlab中實(shí)現(xiàn)主成分分析可以采取兩種方式實(shí)現(xiàn):一是通過(guò)編程來(lái) 實(shí)現(xiàn);二是直接調(diào)用Matlab種自帶程序?qū)崿F(xiàn)。下面主要主要介紹利用 Matlab的 矩陣計(jì)算功能編程實(shí)現(xiàn)主成分分析。1程序結(jié)構(gòu)子函數(shù)Cwpri nt.m主函數(shù)Cwstd.m2函數(shù)FICwfac.mCwscore.mCwstd.m用總和標(biāo)準(zhǔn)化法標(biāo)準(zhǔn)化矩陣Cwfac.m計(jì)算相關(guān)系數(shù)矩陣;計(jì)算特征值和特征向量;對(duì)主成分進(jìn)行排序;計(jì)算各特征值貢獻(xiàn)率;挑選主成分(累計(jì)貢獻(xiàn)率大于85%),輸出主成分個(gè)數(shù);計(jì)算主成分載荷Cwscore.m計(jì)算各主成分得分、綜合得分并排序Cwpri nt.m讀入數(shù)據(jù)

2、文件;調(diào)用以上三個(gè)函數(shù)并輸出結(jié)果3.源程序3.1 cwstd.m總和標(biāo)準(zhǔn)化法標(biāo)準(zhǔn)化矩陣%cwstd.m用總和標(biāo)準(zhǔn)化法標(biāo)準(zhǔn)化矩陣function std=cwstd(vector)cwsum=sum(vector,1);% 對(duì)歹U求和a,b=size(vector);%矩陣大小,a為行數(shù),b為列數(shù)for i=1:afor j=1:bstd(i,j)= vector(i,j)/cwsum(j);end end3.2 cwfac.m 計(jì)算相關(guān)系數(shù)矩陣%cwfac.m function result=cwfac(vector); fprintf(' 相關(guān)系數(shù)矩陣 :n') std=CO

3、RRCOEF(vector)%計(jì)算相關(guān)系數(shù)矩陣fprintf('特征向量(vec)及特征值(val): n') vec,val=eig(std)%求特征值(val)及特征向量(vec)newval=diag(val) ;y,i=sort(newval) ;%對(duì)特征根進(jìn)行排序, y 為排序結(jié)果, i 為索引fprintf(' 特征根排序: n')for z=1:length(y)newy(z)=y(length(y)+1-z);end fprintf('%gn',newy) rate=y/sum(y); fprintf('n 貢獻(xiàn)率: n&

4、#39;) newrate=newy/sum(newy) sumrate=0; newi=;for k=length(y):-1:1 sumrate=sumrate+rate(k); newi(length(y)+1-k)=i(k); if sumrate>0.85 break; endend%記下累積貢獻(xiàn)率大 85%的特征值的序號(hào)放入 newi 中fprintf(' 主成分?jǐn)?shù): %gnn',length(newi);fprintf(' 主成分載荷: n')for p=1:length(newi)for q=1:length(y) result(q,p)=

5、sqrt(newval(newi(p)*vec(q,newi(p);endend%計(jì)算載荷disp(result)3.3 cwscore.m%cwscore.m 計(jì)算得分 function score=cwscore(vector1,vector2); sco=vector1*vector2;csum=sum(sco,2);n ewcsum,i=sort(-1*csum);n ewi,j=sort(i);fprintf('計(jì)算得分:n')score=sco,csum,j%得分矩陣:sco為各主成分得分;csum為綜合得分;j為排序結(jié)果3.4 cwpri nt.m%cwpri n

6、t.mfun cti on prin t=cwpri nt(file name,a,b);%filename為文本文件文件名,a為矩陣行數(shù)(樣本數(shù)),b為矩陣列數(shù)(變量 指標(biāo)數(shù))fid=fope n(filen ame,'r')vector=fsca nf(fid,'%g',a b);fprintf('標(biāo)準(zhǔn)化結(jié)果如下:n')v仁cwstd(vector)result=cwfac(v1); cwscore(v1,result);4.程序測(cè)試?yán)}4.1原始數(shù)據(jù)中國(guó)大陸35個(gè)大城市某年的10項(xiàng)社會(huì)經(jīng)濟(jì)統(tǒng)計(jì)指標(biāo)數(shù)據(jù)見(jiàn)下表。城市 名稱年底總?cè)丝冢ㄈf(wàn)人)非

7、農(nóng)業(yè) 人口比(%)農(nóng)業(yè) 總產(chǎn)值 (萬(wàn)元)工業(yè) 總產(chǎn)值(萬(wàn)元)客運(yùn) 總量(萬(wàn)人)貨運(yùn)總量(萬(wàn) 噸)地方財(cái) 政預(yù)算內(nèi)收入(萬(wàn)元)城鄉(xiāng)居 民年底 儲(chǔ)蓄余 額(萬(wàn)元)在崗 職工 人數(shù)(萬(wàn)人)在崗職 工工資 總額(萬(wàn)元)北10.5971 84319 99920452 79026 806410.805 773京249.908427706323562863646301天910.170.5801 50122 6453261 12811 301202.682 254津9136502259317073931343石家0.2332 9186 885217 095875.40929911352 34895.60758

8、 877莊2680768875太299.920.656236 0382 737111203 2773 94388.65654 023原3750937895100呼和浩207.780.441365 343816 45222105 7831 39642.11309 337特2351623588沈677.080.6291 2955 826715567 9199 016135.451 152濱 上 海 南 京 杭 州/寧 波 合 肥 福 州 廈 門青 島 鄭 州 武 漢 長(zhǎng) 沙 廣 州 深 圳 南/寧545.31691.23927.091313.12537.44616.05538.41429.9558

9、3.13128.99424.20557.63702.97615.36740.20582.47685.00119.85285.8794187337824129980.4941 8798 4261019709 2277 55694.1567393857801877960.4061 8535 96649357 0964 803102.6382103438105327440.4622 6634 18667481 4436 450172.7978551237205200200.7382 06954 5296444 31825 971336.8440190984064855002000.534989 19

10、913 0721426911193664 2995 680113.8117374720.3551 41412 0001711449 5937 42596.9067377968836849670.2541 42810 6222210501 7235 24662.1572358662152983500.3182 514411 622628 764893517233 62847.2741259310.2732 1526 55587467 5245 03069.5932883518511902200.486333 3745 75132418 7582 10846.9351247285703310.39

11、82 305332 6408688 289881674189167 71446062.080.4081 4866 285511460 6904 12683.3153028829157759700.3692 38211 4921317658 4354 978103.5233200364080380450.342677 4255 287106387 2525 13584.6646014337683380.5861 2117 5069155 7489291085793442604 658055149.200.3101 1463 09885323 6603 46169.5773671797067182

12、440.6211 60023 34822231 76120 401182.8147381390078544998110.793299 66220 368841 8479 51991.2612957542749089000.406720 4861 14953149 7002 19045.094691130293918965 922884 4471 3091515 6054451 3578611 180947824 034369 577680 607657 484479 ,555756 696961 704696 8481 314766596 9863 0475941 890338371 809海

13、0.835521 626口54.3844 815717 461115 17419.01198 1384345356800重30.2064 1688 5855225898,9129 090223.731 606慶072.347780525441124969804成10.3351 9355 8944019561 1897 479132.891 200都003.56590289140632684671貴0.4552 2471541 787321.50362 061934143197 90855.28419 681陽(yáng)7703748昆473.390.386793 3563 605512524 2164

14、12788.11842 321明5729604042900西674.500.409739 9053 665109408 8965 863114.01885 169安49423117669800.5442 940142 641州287.59259 444832749169 54065.83550 8905884568西/寧133.950.522765 848711 3101746146949 134855 05127.21219 251銀 川95.380.5709171 603661 2262106119374 758814 10323.72178 621烏魯木0.8241 847292 365

15、158.9278 513668254 87055.27517 622齊42410415084.2運(yùn)行結(jié)果>> cwpri nt('cwbook.txt',35,10) fid =6數(shù)據(jù)標(biāo)準(zhǔn)化結(jié)果如下:v1 =0.05810.03560.04350.06800.05570.11120.11940.11840.10830.13920.04230.03460.03540.07700.00890.06420.04830.04990.05340.05440.04070.01390.06880.02340.00800.00470.01510.03140.02520.01830.

16、01390.03910.00560.00930.00530.02900.00870.01740.02340.01580.00970.02630.00860.00280.00640.00640.00450.00620.01110.00750.03150.03750.03050.01980.02130.03760.02430.03980.03570.02780.02530.02950.04430.02860.02950.04680.03040.03340.02480.02330.03210.02130.02420.04370.02030.01320.02330.01530.02120.02700.

17、04310.03160.02760.06280.01420.01840.01840.02060.02850.04550.06100.13520.04400.04880.18530.01760.10860.18480.11480.08880.02500.03270.03180.02330.04440.03910.02730.02840.02510.03000.02860.02850.02120.03340.04080.04900.02850.01920.03280.02550.02500.01990.01520.03370.03610.06090.02510.02150.02320.01640.

18、02000.00890.01900.01480.00850.01340.00370.01000.00720.01250.02710.01640.01630.05080.02230.02430.01750.02000.02220.01830.00600.01590.02900.00790.01950.01020.00630.01790.00930.01240.01970.01160.02370.01620.00780.01010.00780.00720.01170.01640.02590.01820.02430.03500.02140.01620.02870.01970.01820.02200.

19、03270.02320.02200.05620.03910.03670.04160.02820.02200.02730.02860.01680.02040.01600.01800.02860.01650.01660.02270.02230.03440.03170.03490.02860.02550.02680.03770.02590.02540.03930.02710.01440.01850.02700.01050.02390.01400.01390.01530.01830.03180.07350.03700.03770.07930.06030.05820.07540.09010.04820.

20、00560.04560.04720.00710.06920.02400.01040.07910.04210.02400.01330.00900.02420.01700.00390.01410.00800.00640.00970.01190.00250.00480.04970.00110.00240.01460.00570.00490.00720.00500.14280.03870.01230.09830.02920.14370.06130.03850.04020.05900.04660.02900.01990.04560.02000.11000.04790.02400.03310.03500.

21、01490.02710.00850.00760.04300.01010.00850.00790.01460.01010.02200.02300.01870.01230.01540.02940.02240.01820.02320.02030.03130.02440.01740.01250.02830.02380.01750.02590.03000.02130.01340.03240.00610.01000.00500.01160.00730.01170.01730.01330.00620.03110.00160.00240.00480.00360.00210.00380.00720.00530.

22、00440.03400.00400.00220.00580.00290.00320.00360.00630.00430.00740.04910.00190.00630.00730.02210.01090.01050.01460.0125相關(guān)系數(shù)矩陣 :std =1.0000-0.34440.84250.3603 0.7390 0.62150.40390.49670.67610.4689-0.34441.0000-0.47500.3096-0.35390.19710.35710.26000.15700.30900.8425-0.47501.00000.3358 0.5891 0.50560.32

23、360.44560.55750.37420.36030.30960.33581.00000.15070.76640.94120.84800.73200.86140.7390-0.35390.58910.1507 1.0000 0.42940.19710.31820.38930.25950.62150.19710.50560.76640.42941.00000.83160.89660.93020.90270.40390.35710.32360.94120.19710.83161.00000.92330.83760.95270.49670.26000.44560.84800.31820.89660

24、.92331.00000.92010.97310.67610.15700.55750.73200.38930.93020.83760.92011.00000.93960.46890.30900.37420.86140.25950.90270.95270.97310.9396 1.0000特征向量 (vec):vec =-0.13670.2282-0.26280.19390.6371-0.21630.3176-0.1312-0.41910.2758-0.0329-0.02170.00090.0446-0.1447-0.44370.4058-0.55620.54870.0593-0.0522-0.

25、02800.2040-0.0492-0.5472-0.42250.34400.3188-0.44380.2401 0.0067-0.4176-0.2856-0.23890.1926-0.4915-0.41890.27260.20650.3403 0.0404 0.14080.08960.0380-0.1969-0.0437-0.4888-0.6789 -0.4405 0.1861-0.03430.23600.0640-0.82940.03770.26620.1356-0.12900.02780.37820.29810.47390.56850.23580.1465-0.1502-0.26310.

26、12450.21520.36440.15670.3464-0.64850.2489-0.40430.2058-0.07040.04620.12140.38120.4879-0.57070.1217 0.1761 0.0987 0.35500.3280-0.01390.00710.3832-0.7894-0.16280.19250.2510-0.04220.26940.03960.04560.1668 0.3799特征值 (val)val =0.003900000000000.024000000000000.030700000000000.099100000000000.123200000000

27、000.256600000000000.320700000000000.530000000000002.351400000000006.2602 特征根排序: 6.26022 2.35138 0.530047 0.320699 0.256639 0.123241 0.09909150.03070880.02403550.00393387各主成分貢獻(xiàn)率:0.0257 0.0123 0.00990.0031newrate =0.6260 0.2351 0.0530 0.03210.0024 0.0004第一、二主成分的載荷:0.690 1 -0.64270.148 3 0.84140.600 7 -0.68050.851 5 0.31670.465 6 -0.67540.946 3 0.04260.911 7 0.32990.953 7 0.18620.958 9 0.01090.950 6 0.2558 第一、二、三、四主成分的

溫馨提示

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

評(píng)論

0/150

提交評(píng)論