網站首頁 健康小知識 母嬰教育 起名 運動知識 職場理財 情感生活 綠色生活 遊戲數碼 美容 特色美食 愛好
當前位置:酷知知識幫 > 遊戲數碼 > 電腦

matlab常用函數之format函數short/long/shorE等

欄目: 電腦 / 發佈於: / 人氣:2.02W

Matlab的名稱來源於“矩陣實驗室”,其對矩陣的操作具有先天性的優勢(特別是相對於C語言的數組來説),被廣泛的應用於科學計算,包括數值微積分、複雜系統的仿真,線性/非線性擬合等等舉不勝舉。對於初學者,往往對一些矩陣的簡單的操作不知道對應的函數是什麼,有可能費很大的周折,其實matlab提供了一大堆對矩陣的操作本系列文章旨在對一些Matlab中常用的函數進行介紹。

matlab常用函數之format函數short/long/shorE等

操作方法

(01)matlab中format函數用於控制matlab命令窗口中輸出結果的顯示方式和位數。format的調用形式為:formatformat typeformat('type')其中第一個表示採用默認值,後面兩種的type為具體的顯示類型字符串。matlab提供了十幾種type,包括short (默認),long,shertE,longE,shortG,longG,shortEng,longEng,+,bank,hex,rat,compact,loose. 注意這些type不分大小寫,比圖short可以是Short,sHort或SHORT等,format內部會自行進行轉換識別。可以用get(0,'FormatSpacing')來查看當前是compact還是loose或者用get(0,'Format')來查看當前的其他形式。下面進行一一舉例説明。比如執行下面的代碼:clcformat compactget(0,'FormatSpacing')format looseget(0,'FormatSpacing')format shortget(0,'format')format longget(0,'format')

matlab常用函數之format函數short/long/shorE等 第2張

(02)首先來看compact和loose,這兩個與其他形式不一樣,只控制輸出結果顯示的間隔,不影響數據顯示的位數和精度。用get(0,'FormatSpacing')可以顯示當前是compact還是loose.執行下面的代碼:clcformat compactA1=100A2=piA3=rand(3,4)format('loose') %等價於format looseA4=100A5=piA6=rand(3,4)可以看出A1 A2 A3之間沒有空行,是以compact形式顯示,而A3 A4 A5 A6間都有一行空行,以loose形式顯示。

matlab常用函數之format函數short/long/shorE等 第3張

(03)下面是short形式,這是matlab的默認顯示形式,使matlab以short形式顯示的調用方式有formatformat defaultformat('default ')format shortformat('short')5種。matlab幫助文檔對short的解釋為:Scaled fixed-point format, with 4 digits after the decimalpoint. For example, youare displaying a matrix with a wide range of values, consider using shortG. 簡單理解就是保留4位小數。執行下面的代碼:clcrng('default')format shorta1=pia2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')

matlab常用函數之format函數short/long/shorE等 第4張
matlab常用函數之format函數short/long/shorE等 第5張

(04)下面是long形式,這是matlab的默認顯示形式,使matlab以long形式顯示的調用方式有format longformat('long')2種。matlab幫助文檔對short的解釋為:Scaled fixed-point format with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. 簡單理解就是double型浮點數據保留15為小數,single型浮點數據保留7位小數。執行下面的代碼:clcrng('default')format longa1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')

matlab常用函數之format函數short/long/shorE等 第6張
matlab常用函數之format函數short/long/shorE等 第7張

(05)下面是shortE形式,這是matlab的默認顯示形式,使matlab以shortE形式顯示的調用方式有format shortEformat('shortE')2種。matlab幫助文檔對short的解釋為:Floating-point format, with 4 digits after the decimalpoint. For example, 3.1416e+ger-valued floating-pointnumbers with a maximum of 9 digits are not displayed in scientificnotation.簡單理解就是以指數形式顯示,浮點數底數保留4為小數,整數值小數小於等於9位不以指數形式顯示。執行下面的代碼:clcrng('default')format shortea1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')a8_f=single(a8)a14_f=single(a14)

matlab常用函數之format函數short/long/shorE等 第8張
matlab常用函數之format函數short/long/shorE等 第9張

(06)下面是longE形式,這是matlab的默認顯示形式,使matlab以longE形式顯示的調用方式有format longEformat('longE')2種。matlab幫助文檔對short的解釋為:Floating-point format, with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+ger-valued floating-point numberswith a maximum of 9 digits are not displayed in scientific notation.簡單理解就是以指數形式顯示,double浮點數底數保留15為小數,single型浮點數保留7位小數,整數值小數小於等於9位不以指數形式顯示。執行下面的代碼:clcrng('default')format shortea1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')a8_f=single(a8)a14_f=single(a14)

matlab常用函數之format函數short/long/shorE等 第10張
matlab常用函數之format函數short/long/shorE等 第11張

(07)下面是shortG形式,這是matlab的默認顯示形式,使matlab以shortG形式顯示的調用方式有format shortGformat('shortG')2種。matlab幫助文檔對short的解釋為:Fixed- or floating-point, whichever is more readable,with  4 digits after the decimal point. For example, xample 5, for a comparison between this format and ger-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.簡單理解就是這個是以short和shortE中最優的形式,整數值小數小於等於9位不以指數形式顯示。執行下面的代碼(可以比較short和shortG的執行結果):clcformat shortx = [25 56 255 9876899999]format shortexformat shortgx

matlab常用函數之format函數short/long/shorE等 第12張

(08)下面是longG形式,這是matlab的默認顯示形式,使matlab以longG形式顯示的調用方式有format longGformat('longG')2種。簡單理解就是這個是以long和longE中最優的形式顯示,這裏不再贅述。

(09)下面是shortEng形式和longEng是對應short和long的兩種工程計數顯示,也就是説指數是3的倍數,我們常説的千、兆等。執行下面的代碼:clcformat shortEngget(0,'format')A = 5.123456789;for k=1:10disp(A)A = A * 10;endformat longEngget(0,'format')A = 5.123456789;for k=1:10disp(A)A = A * 10;end可以看出結果的指數是0,3,6,9等

matlab常用函數之format函數short/long/shorE等 第13張
matlab常用函數之format函數short/long/shorE等 第14張

(10)+,bank,hex,rat樣式分別表示顯示結果的正負號(負數顯示"-",正數顯示“+”,0顯示空),保留兩位小數(銀行的角和分),十六進制顯示和有理數(分數)顯示。clcformat +a1=pia2=-pia3=0format banka3=pia4=1/3format hexa5=intmax('int64')a6=intmin('int64')a7=piformat rata8=1/3a10=pi

matlab常用函數之format函數short/long/shorE等 第15張

(11)最後需要注意上面的format函數只是對當前matlab程序的顯示進行設置,下次打開matlab還是原來的設置。要是設置永遠生效,要在在matlab preference菜單中設置,具體是file-->preference...然後如圖

matlab常用函數之format函數short/long/shorE等 第16張