Valid Combinations of Unlike Classes
Matrices and arrays can be composed of elements of almost any MATLAB® data type as long as all elements in the matrix are of the same type. If you do include elements of unlike classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type.
Data type conversion is done with respect to a preset precedence of classes. The following table shows the five classes you can concatenate with an unlike type without generating an error (that is, with the exception of character and logical).
TYPE | character | integer | single | double | logical |
---|---|---|---|---|---|
character | character | character | character | character | invalid |
integer | character | integer | integer | integer | integer |
single | character | integer | single | single | single |
double | character | integer | single | double | double |
logical | invalid | integer | single | double | logical |
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.