📜  MATLAB 中的数值类型

📅  最后修改于: 2022-05-13 01:54:48.663000             🧑  作者: Mango

MATLAB 中的数值类型

MATLAB 中的 Numeric 类包括有符号和无符号整数、单精度浮点数和双精度浮点数。通常,MATLAB 将所有数值存储为双精度浮点数。但是,我们可以选择将任何数字或数字数组存储为整数或单精度,以获得更好的内存利用率。所有数值类型都允许基本的数组操作,例如索引等。

创建数值变量:

Data typeDescriptionSizeTypical range
double      

Double-precision 

(floating-point values)

8 byte       +/- 1.7e +/- 308 (~15 digits)
single

Single-precision 

(floating-point values)

4 byte+/- 3.4e +/- 38 (~7 digits)
int88 bit signed integer1 byte-128 to 127
int1616 bit signed integer2 byte-32768 to 32767
int3232 bit signed integer4 byte-2147483648 to 2147483647
int64Signed integer8 byte-(263) to (263)-1
uint16Unsigned integer2 byte0 to 65535
uint32Unsigned integer4 byte0 to 4294967295
uint64Unsigned integer8 byte0 to 18,446,744,073,709,551,615

示例 1

Matlab
% MATLAB code for Numeric Variables
gfg = single([4.4 3.9 6.9]) .* 8.4
gfg = double([4.4 3.9 6.9]) .* 8.4
gfg = int8([4.4 3.9 6.9]) .* 8.4
gfg = int16([4.4 3.9 6.9]) .* 8.4
gfg = int32([4.4 3.9 6.9]) .* 8.4
gfg = int64([4.4 3.9 6.9]) .* 8.4


Matlab
% MATLAB code for conversion of Numeric Types
gfg = int8([-5 5]);
b= cast(gfg, "uint8")


Matlab
% MATLAB code for conversion of Numeric Types
gfg = int16(-1)
X = typecast(gfg , 'uint16')


Matlab
% MATLAB code for Query Type and Value
gfg = isinteger(4)
g = isfloat(pi)
fg = isreal(2 + 7i)
x = isfinite(1 ./0)
y = isinf(1./0)
z = isnan(0./0)


Matlab
% MATLAB code for Numeric Value Limits
gfg = flintmax
gfg = intmax
gfg = Inf
gfg = intmin
gfg =NaN
gfg = realmax



输出:

gfg = 36.960  32.760   57.960
gfg = 36.960  32.760   57.960
gfg = 34  34  59
gfg = 34  34  59
gfg = 34  34  59
gfg = 34  34  59

数值类型的转换:

MATLAB 提供以下函数来转换为各种数值数据类型:

Data typeDescriptionSyntax
cast    Convert data type of one variable to other
  • B = cast ( A, newclass)
  • B = cast (A , ‘like’,p)
typecastConvert data type without changing underlying data
  • Y = typecast (X, type )

示例 2:

MATLAB

% MATLAB code for conversion of Numeric Types
gfg = int8([-5 5]);
b= cast(gfg, "uint8")

输出:

Ans: b = 0  5

示例 3:

MATLAB

% MATLAB code for conversion of Numeric Types
gfg = int16(-1)
X = typecast(gfg , 'uint16')


输出:

gfg = -1
X = 65535

查询类型和值:

MATLAB 提供以下数据类型来检查各种数值:

Data typeDescriptionSyntax
isinteger        Determine whether input is integer array  TF = isinteger(A) 
isfloatDetermine if input is floating-point arrayTF = isfloat(A)
isnumericDetermine whether input is numeric arrayTF = isnumeric(A)
isrealDetermine whether array uses complex storageTF = isreal(A)
isfiniteDetermine which array elements are finiteTF = isfinite(A)
isinfDetermine which array elements are infiniteTF = isinf(A)
isnanDetermine which array elements are NaN(Not a Number)TF = isnan(A)

示例 4:

MATLAB

% MATLAB code for Query Type and Value
gfg = isinteger(4)
g = isfloat(pi)
fg = isreal(2 + 7i)
x = isfinite(1 ./0)
y = isinf(1./0)
z = isnan(0./0)


输出:

gfg = 0
g = 1
fg = 0
x = 0
y = 1
z = 1

数值限制:

MATLAB 提供以下类型来检查数值的限制:

Type      Description Syntax
epsFloating-point relative accuracy
  • d = eps
  • d = eps(x)
  • d = eps(datatype)
flintmax

Largest consecutive integer 

in floating-point format

  • f = flintmax
  • f = flintmax(precision)
InfCreate array of all Inf values
  • X = Inf
  • X = Inf(n)
  • X = Inf(sz1,…,szN)
  • X = Inf(sz)
  • X = Inf(___,typename)
  • X = Inf(___,’like’,p)
intmaxLargest value of specific integer type
  • v = intmax
  • v = intmax(type)
intminSmallest value of specified integer type
  • v = intmin
  • v = intmin(‘classname’)
NaNCreate array of all Not a Number values
  • X = NaN
  • X = NaN(n)
  • X = NaN(sz1,…,szN)
  • X = NaN(sz)
  • X =NaN(___,typename)
  • X = NaN(___,’like’,p)
realmaxLargest positive floating-point number
  • f = realmax
  • f = realmax(precision)
realmin    Smallest normalized floating-point number
  • f = realmin
  • f = realmin(precision)

示例 5:

MATLAB

% MATLAB code for Numeric Value Limits
gfg = flintmax
gfg = intmax
gfg = Inf
gfg = intmin
gfg =NaN
gfg = realmax


输出:

gfg = 9.0072e+15
gfg = 2147483647
gfg = Inf
gfg = -2147483648
gfg = NaN
gfg = 1.7977e+308