MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Combining Unlike Integer Types


In this section...
“Overview” on page 15-3
“Example of Combining Unlike Integer Sizes” on page 15-3
“Example of Combining Signed with Unsigned” on page 15-4

Overview


If you combine different integer types in a matrix (e.g., signed with unsigned, or 8-bit
integers with 16-bit integers), MATLAB returns a matrix in which all elements are of one
common type. MATLAB sets all elements of the resulting matrix to the data type of the
left-most element in the input matrix. For example, the result of the following
concatenation is a vector of three 16-bit signed integers:

A = [int16(450) uint8(250) int32(1000000)]

Example of Combining Unlike Integer Sizes


After disabling the integer concatenation warnings as shown above, concatenate the
following two numbers once, and then switch their order. The return value depends on
the order in which the integers are concatenated. The left-most type determines the data
type for all elements in the vector:

A = [int16(5000) int8(50)]
A =
5000 50

B = [int8(50) int16(5000)]
B =
50 127

The first operation returns a vector of 16-bit integers. The second returns a vector of 8-bit
integers. The element int16(5000) is set to 127 , the maximum value for an 8-bit signed
integer.

The same rules apply to vertical concatenation:

C = [int8(50); int16(5000)]
C =

Combining Unlike Integer Types
Free download pdf