Integers
In this section...
“Integer Classes” on page 4-2
“Creating Integer Data” on page 4-2
“Arithmetic Operations on Integer Classes” on page 4-4
“Largest and Smallest Values for Integer Classes” on page 4-4
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:
Class
Range of Values
Conversion Function
Signed 8-bit integer
-2
7
to 2
7
-1
int8
Signed 16-bit integer
-2
15
to 2
15
-1
int16
Signed 32-bit integer
-2
31
to 2
31
-1
int32
Signed 64-bit integer
-2
63
to 2
63
-1
int64
Unsigned 8-bit integer
0 to 2
8
-1
uint8
Unsigned 16-bit integer
0 to 2
16
-1
uint16
Unsigned 32-bit integer
0 to 2
32
-1
uint32
Unsigned 64-bit integer
0 to 2
64
-1
uint64
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;
int16(x)
4 Numeric Classes
4-2
Précédent

- 146/1450

Suivant