Programming  Creating and Concatenating Matrices

MATLAB is a matrix-based computing environment. All of the data that you enter into MATLAB is stored in the form of a matrix or a multidimensional array. Even a single numeric value like `100` is stored as a matrix (in this case, a matrix having dimensions 1-by-1):

• ```A = 100;

whos A
Name      Size                   Bytes  Class

A         1x1                        8  double array
```

Regardless of the data type being used, whether it is numeric, character, or logical `true` or `false` data, MATLAB stores this data in matrix (or array) form. For example, the string `'Hello World'` is a 1-by-11 matrix of individual character elements in MATLAB. You can also build matrices composed of more complex data types, such as MATLAB structures and cell arrays.

To create a matrix of basic data elements such as numbers or characters, see

To build a matrix composed of other matrices, see

This section also describes

Constructing a Simple Matrix

The simplest way to create a matrix in MATLAB is to use the matrix constructor operator, `[]`. Create a row in the matrix by entering elements (shown as `E` below) within the brackets. Separate each element with a comma or space:

• ```row = [E1, E2, ..., Em]          row = [E1 E2 ... Em]
```

For example, to create a one row matrix of five elements, type

• ```A = [12 62 93 -8 22];
```

To start a new row, terminate the current row with a semicolon:

• ```A = [row1; row2; ...; rown]
```

This example constructs a 3 row, 5 column (or 3-by-5) matrix of numbers. Note that all rows must have the same number of elements:

• ```A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12    62    93    -8    22
16     2    87    43    91
-4    17   -72    95     6
```

The square brackets operator constructs two-dimensional matrices only, (including 0-by-0, 1-by-1, and 1-by-n matrices). To construct arrays of more than two dimensions, see Creating Multidimensional Arrays.

For instructions on how to read or overwrite any matrix element, see Matrix Indexing. Data Structures Specialized Matrix Functions © 1994-2005 The MathWorks, Inc.