Main Content

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. The one exception in the table is that you cannot convert logical values to the char data type.

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.

Related Topics