MATLAB 数字
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() 返回可以用所有类型的整数表示的最大值和最小值 。
这两个函数都以integer数据类型作为参数,例如intmax(int8)或intmin(int64),并返回可以用integer数据类型表示的最大值和最小值。
下面的例子说明了如何获得整数的最小和最大值 :
% 显示最小和最大的带符号的整数
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'))
sprintf函数参考:MATLAB sprintf
结果:
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”调用时,返回您可以用双精度数据类型表示的最大值和最小值。
举例:
% 显示最小和最大的单精度浮点数
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'))
%显示最小和最大的双精度浮点数
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

关注公众号,获取一手资讯
“ MATLAB 数字 ” comments 0