Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Combining Unlike Classes

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 classes when constructing a matrix, MATLAB converts some elements so that all elements of the resulting matrix are of the same type. (See Classes (Data Types) for information on any of the MATLAB classes discussed here.)

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.

Combining Unlike Integer Types

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)]

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 (left-most) 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:

[msg, intcat_msgid] = lastwarn;
warning('off', intcat_msgid);

To reenable the warning so that it will now be displayed, use

warning('on', intcat_msgid);

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 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 =
    50
   127

Example of Combining Signed with Unsigned

Now do the same exercise with signed and unsigned integers. Again, the left-most element determines the data type for all elements in the resulting matrix:

A = [int8(-100) uint8(100)]
A =
   -100   100

B = [uint8(100) int8(-100)]
B =
   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 =
  50    0

Combining Integer and Noninteger Data

If you combine integers with double, single, or logical classes, all elements of the resulting matrix are given the data type of the left-most integer. For example, all elements of the following vector are set to int32:

A = [true pi int32(1000000) single(17.32) uint8(250)]

Empty Matrices

If you construct a matrix using empty matrix elements, the empty matrices are ignored in the resulting matrix:

A = [5.36; 7.01; []; 9.44]
A =
    5.3600
    7.0100
    9.4400

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 8-bit integer).

x = [int8(21) int8(-22) int8(23) pi 45/6]
x =
    21   -22    23     3     7

class(x)
ans =
   int8

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:

x = ['A' 'B' 'C' 68 69 70]
x =
   ABCDEF

class(x)
ans =
   char

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:

x = [true false false pi sqrt(7)]
x =
    1.0000         0         0    3.1416    2.6458

class(x)
ans =
   double
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS