50
127
Note You can find the maximum or minimum values for any MATLAB integer type using the intmax
and intmin functions. For floating-point types, use realmax and realmin.
Example of Combining Signed with Unsigned
Now do the same exercise with signed and unsigned integers. Again, the leftmost element determines
the data type for all elements in the resulting matrix:
A = [int8(-100) uint8(100)]
A =
1×2 int8 row vector
-100
100
B = [uint8(100) int8(-100)]
B =
1×2 uint8 row vector
100
0
The element int8(-100) is set to zero because it is no longer signed.
MATLAB evaluates each element prior to concatenating them into a combined array. In other words,
the following statement evaluates to an 8-bit signed integer (equal to 50) and an 8-bit unsigned
integer (unsigned -50 is set to zero) before the two elements are combined. Following the
concatenation, the second element retains its zero value but takes on the unsigned int8 type:
A = [int8(50), uint8(-50)]
A =
1×2 int8 row vector
50
0
See Also
More About
•
“Integers” on page 4-2
•
“Integer Arithmetic” on page 4-19
15 Combining Unlike Classes
15-4
127
Note You can find the maximum or minimum values for any MATLAB integer type using the intmax
and intmin functions. For floating-point types, use realmax and realmin.
Example of Combining Signed with Unsigned
Now do the same exercise with signed and unsigned integers. Again, the leftmost element determines
the data type for all elements in the resulting matrix:
A = [int8(-100) uint8(100)]
A =
1×2 int8 row vector
-100
100
B = [uint8(100) int8(-100)]
B =
1×2 uint8 row vector
100
0
The element int8(-100) is set to zero because it is no longer signed.
MATLAB evaluates each element prior to concatenating them into a combined array. In other words,
the following statement evaluates to an 8-bit signed integer (equal to 50) and an 8-bit unsigned
integer (unsigned -50 is set to zero) before the two elements are combined. Following the
concatenation, the second element retains its zero value but takes on the unsigned int8 type:
A = [int8(50), uint8(-50)]
A =
1×2 int8 row vector
50
0
See Also
More About
•
“Integers” on page 4-2
•
“Integer Arithmetic” on page 4-19
15 Combining Unlike Classes
15-4
