MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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 =

int16

1427

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

str = 'Hello World';

int8(str)
ans =

1×11 int8 row vector

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,

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


x = uint32([132 347 528]) .* uint32(75);
class(x)

4 Numeric Classes

Free download pdf