This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


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 8-byte 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 32-bit 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:


Range of Values

Conversion Function

Signed 8-bit integer

-27 to 27-1


Signed 16-bit integer

-215 to 215-1


Signed 32-bit integer

-231 to 231-1


Signed 64-bit integer

-263 to 263-1


Unsigned 8-bit integer

0 to 28-1


Unsigned 16-bit integer

0 to 216-1


Unsigned 32-bit integer

0 to 232-1


Unsigned 64-bit integer

0 to 264-1


Creating Integer Data

MATLAB stores numeric data as double-precision 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 16-bit 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 from the two equally nearby integers, MATLAB chooses the one for which the absolute value is larger in magnitude:

x = 325.499;                  x = x + .001;

int16(x)                      int16(x)
ans =                         ans =
     325                           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;

ans =

Arithmetic operations that involve both integers and floating-point 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 =

The integer conversion functions are also useful when converting other classes, such as strings, to integers:

str = 'Hello World';

ans =
   72  101  108  108  111   32   87  111  114  108  100

If you convert a NaN value into an integer class, the result is a value of 0 in that integer class. For example,


ans =

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. This yields a result that has the same data type as the operands:

    x = uint32([132 347 528]) .* uint32(75);
    ans =
  • Integers or integer arrays and scalar double-precision floating-point numbers. This yields a result that has the same data type as the integer operands:

    x = uint32([132 347 528]) .* 75.49;
    ans =

For all binary operations in which one operand is an array of integer data type (except 64-bit integers) and the other is a scalar double, MATLAB computes the operation using elementwise double-precision arithmetic, and then converts the result back to the original integer data type. For binary operations involving a 64-bit integer array and a scalar double, MATLAB computes the operation as if 80-bit extended-precision arithmetic were used, to prevent loss of precision.

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 Integers 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')                intmin('int8')
ans =                         ans =
   127                           -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(-300)
x =                           x =
   127                           -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(-100) * 3
x =                           x =
   127                           -128

Integer Functions

See Integer Functions for a list of functions most commonly used with integers in MATLAB.

Was this topic helpful?