Note that the semicolon (;) identifies the end of a row in the matrix. Commas may be used to separate columns in the matrix, but they are not necessary.

Matrices may be created directly in much the same way as vectors are. As an example, let's build the matrix
\[
A = \left[\begin{smallmatrix} 2 & 4 \\ 6 & 8 \\ 10 & 12 \end{smallmatrix}\right].
\]
This may be done in several ways.

A = [24; 68; 1012];
A = [24681012];

Note that a semicolon (;) separates each row of the matrix while spaces separate each column.

We may also create a matrix from vectors. For example, we may construct A from its rows as follows:

Accessing Arrays

Often we need to access certain elements or groups of elements from an array. There are a few ways to do this.

Indexing arrays

Recall that an array is simply a collection of numbers. Arrays may be 1-dimensional (vectors), 2-dimensional (matrices) or even higher dimensional. Each number in an array has a unique location identified by an index. Array indexing is best illustrated by example.

In general for a two-dimensional array, B(i,j) refers to the ith row and jth column of B.
This is extensible to arrays of higher dimensions as well.

If we return to our previous example with the B matrix, we find something interesting:

B = [529; 173; 260];
b = B(5);

What should the value of b be? One might expect this to result in an error, since B is not a vector. However, this returns b=7. What is happening? Matlab actually treats a matrix as a long vector, B = [ 5 2 9 1 7 3 2 6 0 ], with each row stacked next to the previous row. Therefore, B(5) says "go to the fifth entry" counted along rows.

Slicing arrays using the ":" operator

Sometimes we need to grab a group of elements from an array. We can do this using the : operator For example:

Cell Arrays

The arrays we have discussed up to this point can hold a single type of information. For example, each element of the array can hold a single number. They could also hold a text string, as long as each text string was the same length.

Cell arrays are special kinds of arrays that can hold any type of information. For example:

a = {1, 'hi there', [543]};

Cell arrays are built in much the same way as a normal array with one important difference: we use { and } rather than [ and ] to construct the array. Accessing cell arrays is also different. For example, given the cell array a defined above, we could access its elements in the following way: