Integers
Integer Classes
MATLAB^{®} has four signed and four unsigned integer classes. Signed types enable you to work with negative integers as well as positive, but cannot represent as wide a range of numbers as the unsigned types because one bit is used to designate a positive or negative sign for the number. Unsigned types give you a wider range of numbers, but these numbers can only be zero or positive.
MATLAB supports 1, 2, 4, and 8byte storage for integer data. You can save memory and execution time for your programs if you use the smallest integer type that accommodates your data. For example, you do not need a 32bit integer to store the value 100
.
Here are the eight integer classes, the range of values you can store with each type, and the MATLAB conversion function required to create that type.
Class  Range of Values  Conversion Function 

Signed 8bit integer  –2^{7} to 2^{7}–1 

Signed 16bit integer  –2^{15} to 2^{15}–1 

Signed 32bit integer  –2^{31} to 2^{31}–1 

Signed 64bit integer  –2^{63} to 2^{63}–1 

Unsigned 8bit integer  0 to 2^{8}–1 

Unsigned 16bit integer  0 to 2^{16}–1 

Unsigned 32bit integer  0 to 2^{32}–1 

Unsigned 64bit integer  0 to 2^{64}–1 

Creating Integer Data
MATLAB stores numeric data as doubleprecision floating point (double
) by default. To store data as an integer, you need to convert from double
to the desired integer type. Use one of the conversion functions shown in the table above.
For example, to store 325
as a 16bit signed integer assigned to variable x
, type
x = int16(325);
If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest integer. If the fractional part is exactly 0.5
, then MATLAB chooses the nearest integer whose absolute value is larger in magnitude:
x = 325.499; int16(x)
ans = int16 325
x = x + .001; int16(x)
ans = int16 326
If you need to round a number using a rounding scheme other than the default, MATLAB provides four rounding functions: round
, fix
, floor
, and ceil
. The fix
function enables you to override the default and round towards zero when there is a nonzero fractional part:
x = 325.9; int16(fix(x))
ans = int16 325
Arithmetic operations that involve both integers and floatingpoint numbers always result in an integer data type. MATLAB rounds the result, when necessary, according to the default rounding algorithm. The example below yields an exact answer of 1426.75
which MATLAB then rounds to the next highest integer:
int16(325)*4.39
ans = int16 1427
The integer conversion functions are also useful when converting other classes, such as character vectors, to integers:
chr = 'Hello World';
int8(chr)
ans = 1×11 int8 row vector 72 101 108 108 111 32 87 111 114 108 100
If you convert a NaN
value to an integer class, the result is a value of 0
in that integer class. For example:
int32(NaN)
ans = int32 0
Arithmetic Operations on Integer Classes
MATLAB can perform integer arithmetic on the following types of data:
Integers or integer arrays of the same integer data type. Arithmetic operations yield a result that has the same data type as the operands:
x = uint32([132 347 528]) .* uint32(75); class(x)
ans = 'uint32'
Integers or integer arrays and scalar doubleprecision floatingpoint numbers. Arithmetic operations yield a result that has the same data type as the integer operands:
x = uint32([132 347 528]) .* 75.49; class(x)
ans = 'uint32'
For all binary operations in which one operand is an array of integer data type (except 64bit integers) and the other is a scalar double, MATLAB computes the operation using elementwise doubleprecision arithmetic, and then converts the result back to the original integer data type. For binary operations involving a 64bit integer array and a scalar double, MATLAB computes the operation as if 80bit extendedprecision arithmetic were used, to prevent loss of precision.
Operations involving complex numbers with integer types are not supported.
Largest and Smallest Values for Integer Classes
For each integer data type, there is a largest and smallest number that you can represent with that type. The table shown under Integer Classes lists the largest and smallest values for each integer data type in the “Range of Values” column.
You can also obtain these values with the intmax
and intmin
functions:
intmax("int8")
ans = int8 127
intmin("int8")
ans = int8 128
If you convert a number that is larger than the maximum value of an integer data type to that type, MATLAB sets it to the maximum value. Similarly, if you convert a number that is smaller than the minimum value of the integer data type, MATLAB sets it to the minimum value. For example:
x = int8(300)
x = int8 127
x = int8(300)
x = int8 128
Also, when the result of an arithmetic operation involving integers exceeds the maximum (or minimum) value of the data type, MATLAB sets it to the maximum (or minimum) value:
x = int8(100)*3
x = int8 127
x = int8(100)*3
x = int8 128
Loss of Precision Due to Conversion
When you create a numeric array of large integers (larger than flintmax
), MATLAB initially represents the input as double precision by default. Precision can be lost when you convert this input to the int64
or uint64
data type. To maintain precision, call int64
or uint64
with each scalar element of the array instead.
For example, convert a numeric array of large integers to a 64bit signed integer array by using int64
. The output array loses precision.
Y_inaccurate = int64([72057594035891654 81997179153022975])
Y_inaccurate = 1×2 int64 row vector
72057594035891656 81997179153022976
Instead, call int64
with each scalar element to return an accurate array.
Y_accurate = [int64(72057594035891654) int64(81997179153022975)]
Y_accurate = 1×2 int64 row vector
72057594035891654 81997179153022975
You can also create the integer array without loss of precision by using the hexadecimal or binary values of the integers. (since R2019b)
Y_accurate = [0xFF000000001F123As64 0x1234FFFFFFFFFFFs64]
Y_accurate = 1×2 int64 row vector
72057594035891654 81997179153022975