MATLAB - 數字



MATLAB 支援各種數值類,包括帶符號和無符號整數以及單精度和雙精度浮點數。預設情況下,MATLAB 將所有數值儲存為雙精度浮點數。

您可以選擇將任何數字或數字陣列儲存為整數或單精度數。

所有數值型別都支援基本陣列運算和數學運算。

轉換為各種數值資料型別

MATLAB 提供以下函式來轉換為各種數值資料型別 -

函式 用途
double 轉換為雙精度數
single 轉換為單精度數
int8 轉換為 8 位有符號整數
int16 轉換為 16 位有符號整數
int32 轉換為 32 位有符號整數
int64 轉換為 64 位有符號整數
uint8 轉換為 8 位無符號整數
uint16 轉換為 16 位無符號整數
uint32 轉換為 32 位無符號整數
uint64 轉換為 64 位無符號整數

示例

建立一個指令碼檔案並輸入以下程式碼 -

x = single([5.32 3.47 6.28]) .* 7.5
x = double([5.32 3.47 6.28]) .* 7.5
x = int8([5.32 3.47 6.28]) .* 7.5
x = int16([5.32 3.47 6.28]) .* 7.5
x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5

執行檔案時,它顯示以下結果 -

x =

   39.900   26.025   47.100

x =

   39.900   26.025   47.100

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

示例

讓我們進一步擴充套件前面的示例。建立一個指令碼檔案並輸入以下程式碼 -

x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x = num2cell(x)

執行檔案時,它顯示以下結果 -

x =

   38  23  45

x =

   38  23  45

x = 
{
   [1,1] = 38
   [1,2] = 23
   [1,3] = 45
}

最小和最大整數

函式 intmax()intmin() 返回所有型別的整數可以表示的最大值和最小值。

這兩個函式都將整數資料型別作為引數,例如 intmax(int8) 或 intmin(int64),並返回可以用整數資料型別表示的最大值和最小值。

示例

以下示例說明了如何獲取整數的最小值和最大值。建立一個指令碼檔案並在其中編寫以下程式碼 -

% displaying the smallest and largest signed integer data
str = 'The range for int8 is:\n\t%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))

str = 'The range for int16 is:\n\t%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))

str = 'The range for int32 is:\n\t%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))

str = 'The range for int64 is:\n\t%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
 
% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:\n\t%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))

str = 'The range for uint16 is:\n\t%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))

str = 'The range for uint32 is:\n\t%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))

str = 'The range for uint64 is:\n\t%d to %d ';
sprintf(str, intmin('uint64'), intmax('uint64'))

執行檔案時,它顯示以下結果 -

ans = The range for int8 is:
	-128 to 127 
ans = The range for int16 is:
	-32768 to 32767 
ans = The range for int32 is:
	-2147483648 to 2147483647 
ans = The range for int64 is:
	0 to 0 
ans = The range for uint8 is:
	0 to 255 
ans = The range for uint16 is:
	0 to 65535 
ans = The range for uint32 is:
	0 to -1 
ans = The range for uint64 is:
	0 to 18446744073709551616 

最小和最大浮點數

函式 realmax()realmin() 返回可以用浮點數表示的最大值和最小值。

當這兩個函式以 'single' 作為引數呼叫時,返回可以用單精度資料型別表示的最大值和最小值,當以 'double' 作為引數呼叫時,返回可以用雙精度資料型別表示的最大值和最小值。

示例

以下示例說明了如何獲取最小和最大浮點數。建立一個指令碼檔案並在其中編寫以下程式碼 -

% displaying the smallest and largest single-precision 
% floating point number
str = 'The range for single is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
   realmin('single'), realmax('single'))

% displaying the smallest and largest double-precision 
% floating point number
str = 'The range for double is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('double'), -realmin('double'), ...
   realmin('double'), realmax('double'))

執行檔案時,它會顯示以下結果 -

ans = The range for single is:                                                  
        -3.40282e+38 to -1.17549e-38 and                                        
         1.17549e-38 to  3.40282e+38                                            
ans = The range for double is:                                                  
        -1.79769e+308 to -2.22507e-308 and                                      
         2.22507e-308 to  1.79769e+308
廣告
© . All rights reserved.