INT32 Convert to Signed 32-bit Integer
Section: Type Conversion Functions
Usage
Converts the argument to an signed 32-bit Integer. The syntax for its use isy = int32(x)
where x
is an n
-dimensional numerical array. Conversion
follows the saturation rules (e.g., if x
is outside the normal
range for a signed 32-bit integer of [-2147483647,2147483647]
,
it is truncated to that range). Note that both NaN
and Inf
both map to 0.
Example
The following piece of code demonstrates several uses ofint32
. First, the routine uses
--> int32(100) ans = 100 --> int32(-100) ans = -100
In the next example, an integer outside the range of the type is passed in. The result is truncated to the range of the data type.
--> int32(40e9) ans = 2147483647
In the next example, a positive double precision argument is passed in. The result is the signed integer that is closest to the argument.
--> int32(pi) ans = 3
In the next example, a complex argument is passed in. The result is the signed complex integer that is closest to the argument.
--> int32(5+2*i) ans = 5.0000 + 2.0000i
In the next example, a string argument is passed in. The string argument is converted into an integer array corresponding to the ASCII values of each character.
--> int32('helo') ans = 104 101 108 111
In the last example, a cell-array is passed in. For cell-arrays and structure arrays, the result is an error.
--> int32({4}) Error: Cannot perform type conversions with this type