📜  MATLAB数据类型

📅  最后修改于: 2021-01-07 02:06:44             🧑  作者: Mango

MATLAB数据类型

MATLAB中的基本数据类型(也称为类)是数组或矩阵。 MATLAB中有15种基本数据类型。这些数据类型中的每一个都在矩阵或数组的内部。此矩阵或数组的大小最小为0 x 0,并且可以增加为任意大小的n维数组。

下表描述了这些数据类型是:

Data Type Example Description
int8, uint8,
int16, uint16,
int32, uint32,
int64, uint64
uint16(65000) The array of signed and unsigned integers. It requires less storage space than single or double. All integer data types except for int64 and uint64 can be used in mathematical operations.
single 3 * 10^38 The array of single-precision numbers. It requires less storage space than double but has less precision and a smaller range.
double 3 * 10^300
5 + 6i
Array of double-precision numbers. Two-dimensional array can be sparse. The default numeric types in MATLAB.
logical magic(4) > 10 Array of logical value of 1 or 0 to represent true and false respectively. Two-dimensional arrays can be sparse.
char ‘Hello’ Array of characters. Strings are represented as vectors of characters. For arrays including more than one string, it is good to use cell arrays.
cell array a{1,1} = 12;
a{1,2} = ‘Red’;
a{1,3} = magic(4);
Array of indexed cells, each capable of saving an array of a various dimension and data type.
structure a.day = 12;
a.color = ‘Red’;
a.mat = magic(3);
Array of C-like structures, each structure having named fields capable of storing an array of a different dimension and data type.
function handle @sin Pointer to a function. You can pass function handles to other functions.
user class polynom([0 -2 -5]) Objects constructed from a user-defined class.
Java class java.awt.Frame Objects constructed from a Java class.

数值类型

MATLAB中的数值数据类型包含有符号和无符号整数,以及单精度和双精度浮点数。整数和单精度数组比双精度数组提供了更高的内存效率存储。

所有数字类型都提供基本的数组功能,例如下标和整形。除int64和uint64之外的所有数字类型都可以在数字运算中使用。

整数

MATLAB具有四个有符号和四个无符号整数数据类型。

带符号的类型既可以使用负数也可以使用正数,但是不能执行与无符号类型相同的数字范围,因为使用一位来指定该数字的正号或负号。

无符号类型可以提供更大范围的数字,但是这些数字只能为零或正数。

MATLAB提供了数字数据的1、2、4和8字节存储。如果使用容纳您数据的最小整数类型,则可以节省程序的内存和执行时间。例如,我们不需要32位整数来保存值100。

这是八个数字数据类型,每种类型可以保存的值范围,以及创建该类型所需的MATLAB转换操作:

Data Type Range of Values Conversion Function
Signed 8-bit integer -27 to 27-1 int8
Signed 16-bit integer -215 to 215-1 int16
Signed 32-bit integer -231 to 231-1 int32
Signed 64-bit integer -263 to 263-1 int64
Unsigned 8-bit integer 0 to 28-1 uint8
Unsigned 16-bit integer 0 to 216-1 uint16
Unsigned 32-bit integer 0 to 232-1 uint32
Unsigned 64-bit integer 0 to 264-1 uint64

创建整数数据

MATLAB默认将数字数据保存为双精度浮点数。要将数据保存为整数,请使用上表中所示的转换函数之一:

   x = int16 (32501);

我们可以使用whos函数显示变量表示的数组的维数,字节数和数据类型:

whos x 
        Name      Size      Bytes      Class 
         x        1x1        2        int16 array    

如果要分配输出,可以使用class函数,如下所示:

xType = class(x) 
xType = 
         int16

如果只想验证x是整数,请使用isinteger函数:

isinteger(x) 
ans = 
    1    

整数函数

Function Description
int8, int16,
int32, int64
It convert to signed 1-, 2-, 4-, or 8-byte integer.
uint8, uint16,
uint32, uint64
It converts to unsigned 1-, 2-, 4-, or 8-byte integer.
class It returns the data type of an object.
isa It determines if the input value has the specified data type.
isinteger It determines if input value is an integer array.
isnumeric It determines if the input value is a numeric array.

浮点数字

MATLAB以双精度或单精度格式显示浮点数。默认值为双精度,但是我们可以使用简单的转换函数使任意数量的单精度。

双精度浮点

MATLAB根据IEEE标准754编写了double数据类型,以实现双精度。存储为双重需要的64位的任何值,其格式如下表所示:

Bits Usage
63 Sign (0 = positive, 1 = negative)
62 to 52 Exponent, biased by 1023
51 to 0 Fraction f of the number 1.f

单精度浮点

MATLAB会根据IEEE标准754编写单一精度的单一数据类型。保存为单个值的任何值都需要32位,格式如下表所示:

Bits Usage
31 Sign (0 = positive, 1 = negative)
30 to 23 Exponent, biased by 127
22 to 0 Fraction f of the number 1.f

浮点函数

Function Description
double It converts to double precision.
single It converts to single precision.
class It returns the data type of an object.
isa It determines if the input value has the specified data type.
isfloat It determines if the input value is a floating-point array.
isnumeric It determines if the input value is a numeric array
eps It returns the floating-point relative accuracy. This value is the tolerance MATLAB uses in its evaluation.
realmax It returns the largest floating-point number your computer can represent.
realmin It returns the smallest floating-point number our computer can represent.

复数

复数由两个独立的部分组成:实部和虚部。初级虚数单位等于-1的平方根。这在MATLAB中以两个字母之一显示:i或j。

创建复数

以下语句显示了一种在MATLAB中创建复杂值的方法。变量x被分配了一个复数,其实部为2,虚部为3:

x = 2 + 3i;

复数函数

Function Description
complex It construct complex data from real and imaginary components.
i or j It returns the imaginary unit used in constructing complex data.
real It returns the real part of a complex number
imag It returns the imaginary part of a complex number.
isreal It determines if a number is real or imaginary.