Programming 
Combining Unlike Data Types
Matrices and arrays can be composed of elements of most any MATLAB data type as long as all elements in the matrix are of the same type. If you do include elements of unlike data types when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type. (See Data Types for information on any of the MATLAB data types discussed here.)
Data type conversion is done with respect to a preset precedence of data types. The following table shows the five data types you can concatenate with an unlike type without generating an error.
For example, concatenating a double
and single
matrix always yields a matrix of type single
. MATLAB converts the double
element to single
to accomplish this.
Combining Unlike Integer Types
If you combine different integer types in a matrix (e.g., signed with unsigned, or 8bit integers with 16bit 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 leftmost element in the input matrix. For example, the result of the following concatenation is a vector of three 16bit signed integers:
MATLAB also displays a warning to inform you that the result may not be what you had expected:
A = [int16(450) uint8(250) int32(1000000)]; Warning: Concatenation with dominant (leftmost) integer class may overflow other operands on conversion to return class.
You can disable this warning by entering the following two commands directly after the operation that caused the warning. The first command retrieves the message identifier associated with the most recent warning issued by MATLAB. The second command uses this identifier to disable any further warnings of that type from being issued:
To reenable the warning so that it will now be displayed, use
You can use these commands to disable or enable the display of any MATLAB warning.
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 leftmost type determines the data type for all elements in the vector:
The first operation returns a vector of 16bit integers. The second returns a vector of 8bit integers. The element int16(5000)
is set to 127
, the maximum value for an 8bit signed integer.
The same rules apply to vertical concatenation:
Note
You can find the maximum or minimum values for any MATLAB integer type using the intmax and intmin functions. For floatingpoint 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:
The element int8(100)
is set to zero because it is no longer signed:
Combining Integer and Noninteger Data
If you combine integers with double
, single
, or logical
data types, all elements of the resulting matrix are given the data type of the leftmost integer. For example, all elements of the following vector are set to int32
:
Empty Matrices
If you construct a matrix using empty matrix elements, the empty matrices are ignored in the resulting matrix:
Concatenation Examples
Here are some examples of data type conversion during matrix construction.
Combining Single and Double Types. Combining single
values with double
values yields a single
matrix. Note that 5.73*10^300
is too big to be stored as a single
, thus the conversion from double
to single
sets it to infinity. (The class
function used in this example returns the data type for the input value):
x = [single(4.5) single(2.8) pi 5.73*10^300] x = 4.5000 2.8000 3.1416 Inf class(x) % Display the data type of x ans = single
Combining Integer and Double Types. Combining integer values with double
values yields an integer matrix. Note that the fractional part of pi
is rounded to the nearest integer. (The int8
function used in this example converts its numeric argument to an 8bit integer):
Combining Character and Double Types. Combining character
values with double
values yields a character
matrix. MATLAB converts the double
elements in this example to their character
equivalents:
Combining Logical and Double Types. Combining logical
values with double
values yields a double
matrix. MATLAB converts the logical
true
and false
elements in this example to double
:
Generating a Numeric Sequence  Matrix Indexing 
© 19942005 The MathWorks, Inc.