Programming Previous page   Next Page

Concatenating Matrices

Matrix concatenation is the process of joining one or more matrices to make a new matrix. The brackets [] operator discussed earlier in this section serves not only as a matrix constructor, but also as the MATLAB concatenation operator. The expression C = [A B] horizontally concatenates matrices A and B. The expression C = [A; B] vertically concatenates them.

This example constructs a new matrix C by concatenating matrices A and B in a vertical direction:

Keeping Matrices Rectangular

You can construct matrices, or even multidimensional arrays, using concatenation as long as the resulting matrix does not have an irregular shape (as in the second illustration shown below). If you are building a matrix horizontally, then each component matrix must have the same number of rows. When building vertically, each component must have the same number of columns.

This diagram shows two matrices of the same height (i.e., same number of rows) being combined horizontally to form a new matrix.

The next diagram illustrates an attempt to horizontally combine two matrices of unequal height. MATLAB does not allow this.


Previous page  Specialized Matrix Functions Matrix Concatenation Functions Next page

© 1994-2005 The MathWorks, Inc.