MATLAB可以選擇存儲任何數(shù)字或數(shù)字為整數(shù)或單精度數(shù)字陣列。
MATLAB所有的數(shù)字類型支持基本的數(shù)組運(yùn)算和數(shù)學(xué)運(yùn)算。
MATLAB各種數(shù)字?jǐn)?shù)據(jù)類型的轉(zhuǎn)換
MATLAB提供各種數(shù)字?jǐn)?shù)據(jù)類型轉(zhuǎn)換為以下功能:
函數(shù) | 目的 |
---|---|
double | 轉(zhuǎn)換為雙精度數(shù)字 |
single | 轉(zhuǎn)換為單精度數(shù) |
int8 | 轉(zhuǎn)換為8位有符號整數(shù) |
int16 | 轉(zhuǎn)換為16位有符號整數(shù) |
int32 | 轉(zhuǎn)換為32位有符號整數(shù) |
int64 | 轉(zhuǎn)換為64位有符號整數(shù) |
uint8 | 轉(zhuǎn)換為8位無符號整數(shù) |
uint16 | 轉(zhuǎn)換為16位無符號整數(shù) |
uint32 | 轉(zhuǎn)換為32位無符號整數(shù) |
uint64 | 轉(zhuǎn)換為64位無符號整數(shù) |
詳細(xì)例子
在MATLAB中建立一個腳本文件,輸入下述代碼:
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
運(yùn)行該文件,顯示以下結(jié)果:
x =
39.9000 26.0250 47.1000
x =
39.9000 26.0250 47.1000
x =
38 23 45
x =
38 23 45
x =
38 23 45
x =
38 23 45
詳細(xì)例子
對前面的例子進(jìn)行擴(kuò)展,建立一個腳本文件,輸入下述代碼:
x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x = num2cell(x)
運(yùn)行該文件,顯示以下結(jié)果:
x =
38 23 45
x =
38 23 45
x =
[38] [23] [45]
最小和最大整數(shù)
MATLAB中使用函數(shù) intmax() 和 intmin() 返回最大和最小的值,它可以表示所有類型的整數(shù)。
這兩個功能整數(shù)數(shù)據(jù)類型作為參數(shù),例如,intmax(int8) 或 intmin(int64) 最大值和最小值值,可以表示的整數(shù)數(shù)據(jù)類型并返回。
詳細(xì)例子
下面的例子說明如何得到最小值和最大值的整數(shù)。
在MATLAB中建立一個腳本文件,輸入下述代碼:
% displaying the smallest and largest signed integer data
str = 'The range for int8 is:
%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))
str = 'The range for int16 is:
%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))
str = 'The range for int32 is:
%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))
str = 'The range for int64 is:
%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:
%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))
str = 'The range for uint16 is:
%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))
str = 'The range for uint32 is:
%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))
str = 'The range for uint64 is:
%d to %d ';
sprintf(str, intmin('uint64'), intmax('uint64'))
運(yùn)行該文件,顯示以下結(jié)果:
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:
-9223372036854775808 to 9223372036854775807
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 4294967295
ans =
The range for uint64 is:
0 to 1.844674e+19
MATLAB最小和最大浮點數(shù)
使用函數(shù) realmax() 和 realmin() 返回的最大值和最小值,可以表示為浮點數(shù)。
這兩個函數(shù)調(diào)用時的參數(shù)'單',返回的最大值和最小值值,可以代表單精度數(shù)據(jù)類型以及何時被稱為'雙'的參數(shù),返回的最大值和最小值值,可以表示雙精度數(shù)據(jù)類型。
詳細(xì)實例
下面的例子說明如何獲得最大和最小的浮點數(shù)。
在MATLAB中建立一個腳本文件,輸入下述代碼:
% displaying the smallest and largest single-precision
% floating w3cschool number
str = 'The range for single is:
%g to %g and
%g to %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
realmin('single'), realmax('single'))
% displaying the smallest and largest double-precision
% floating w3cschool number
str = 'The range for double is:
%g to %g and
%g to %g';
sprintf(str, -realmax('double'), -realmin('double'), ...
realmin('double'), realmax('double'))
運(yùn)行該文件,顯示以下結(jié)果:
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
更多建議: