Arrays are the primary data type in MATLAB. Prior to version 5, the
numeric array was the *only* data type. Version 5 introduced several
new data types, two of which are extensions of the basic two-dimensional
numeric array: the cell array (and the related structure array) and the
multi-dimensional aray. These new data types add significantly to the
flexibility of MATLAB and, while the enhancement to the language is certainly
welcome, you now need to be more aware of the context in which a variable
is used in order to avoid syntax errors.

[Back to main page] [Back to language features]

- Double arrays: arrays of (complex) numbers
- Cell arrays: arrays of "things".
- N-dimensional arrays: Miscellaneous tips and generalizations

The numeric array is the workhorse data type for scientific computation.
The branch of mathematics called linear algebra concentrates on
2-dimensional arrays of numbers called matrices, for example. Because
matrix mathematics and numerics were the motivation for developing the
original version of MATLAB (short for **MAT**rix **LAB**oratory),
two-dimensional arrays of (possibly complex) numbers were the basis for all
computation in MATLAB.

Generalizations to N-dimensional arrays is straightforward but not trivial. Cell arrays are different animals altogether.

Some issues to be aware of when dealing with arrays:

- Values and indices
- Sizing up arrays
- The "rule of rectangularity"
- Element-by-element versus column-oriented functions on arrays

**The value is not the index (and vice-versa)**

Numeric-array variables in MATLAB contain *MN* numbers in an array
with *M* rows and *N* columns. The numbers held by the array are
referred to as the **elements** of the array while the numbers that
indicate the row/column location of an element are referred to as the
**indices** of an element. For example, the array

A = [2 8 1 ; 4 9 5]

has *M* = 2 rows, *N* = 3 columns and so is composed of
*MN* = 6 elements. The element at the intersection of row #2 and
column #3 (row index = 2, column index = 3) has the value 5.

The mathematical notation for a particular element in an array is
*A*_{jk} which stands for "the value of the element at the
intersection of row *j* and column *k* in the array *A*".
In the example above, the element denoted *A*_{23} has the
value 5. In MATLAB notation, the ` j`-

When dealing with and using arrays, it is vital to keep the values of the elements distinct from the indices used to locate them.

There are several utility functions in MATLAB that can provide information on the shape or size of an array. Specifically:

MATLAB function | what it provides |
---|---|

[M,N] = size(A) | extent (dimensions) of the array |

L = length(A) | -
use with vectors onlymax(size(A)) |

s = whos | extents of all arrays in the current workspace |

Note that when used with an output, ** whos** can provide much
detailed information about the variables in a workspace. More commonly

**Keep it rectangular**

All numeric arrays **must** be rectangular which means two things:

**All rows in the array must have the same number of columns****All columns in the array must have the same number of rows**

The rule of rectrangularity is used most commonly encountered when constructing arrays from smaller arrays (or vectors) or when you are trying to assign the values in a portion of an array.

**Applying functions to arrays**

When given an array as input, most of the mathematical functions in
MATLAB (such as ** sin**,

A = [2 8 1 ; 4 9 5] log(A) % element by element

yields an array of the same size as ** A** with element values
found froms the natural logarithm of the corresponding element values in

ans = 0.6931 2.0794 0 1.3863 2.1972 1.6094

There is a subset of MATLAB functions that does not operate on an
element-by-element basis. As a prototype of such functions, consider the
** sum** function applied to the array above:

sum(A) ans = 6 17 6

Functions like ** sum** are called

sum mean std median min sort cumsum trapz prod max

The help information on these functions shows how additional arguments
can be provided that direct which dimension is to be treated as the
"column" dimension.

Cell arrays are the "spreadsheets" of the array world. The elements of a cell array are - well, cells. Cells are generic containers, capable of holding any valid MATLAB object. There is no restriction to a single numerical value in a cell. Rather, anything that fits is allowed.

Cell arrays are similar to the traditional numeric array in one important
way - they must be rectangular (each row has the same number of columns
and each column has the same number of rows). To distinguish a cell
array from a numeric array, braces (** {}**) are used to
delimit the array. Thus, a 1-by-2 cell array can be created by

>> A = [1 2 ; 3 4] % a 2D numeric array >> animal = 'dog' % a string (1D character array) >> C = {A animal} % a 1D cell array with 2 elements C = [2x2 double] 'dog'

Note that the different data types (numbers and letters) are easily
accomodated by the elements of a cell array. The first element of
** C** (also termed the first cell of

However, the variable>> cell1 = C(1) % "The 1,1 cell of C" cell1 = [2x2 double]

One upshot of this change in the class of a array of numbers is that you cannot apply the usual linear algebra functions to a cell array. For example, attempting to invert the what might be considered as the array in>> whos Name Size Bytes Class A 2x2 32 double array C 1x2 222 cell array animal 1x3 6 char array cell1 1x1 124 cell array >> class(cell1) ans = cell

To actually get at the>> inv(cell1) ??? Function 'inv' not defined for variables of class 'cell'.

Getting the syntax right with cell arrays requires that you clearly make the distinction between the containers (elements or cells) and the contents of an array. In summary:>> arr = C{1}; % "The contents of the first cell" >> class(arr) ans = double >> inv(arr) ans = -2.0000 1.0000 1.5000 -0.5000

Indexing method | What you are doing |
---|---|

Parentheses, () | Referring to the container |

Braces, {} | Referring to the contents |

While the syntax is admittedly a bit clumsy, you can address specific portions of the contents of cell arrays by compounding the subscripts. For example,

>> C{1}(1,2) % the 1,2 element of the array contained in the first cell ans = 2 >> C{2}(2) % the second character contained in the second cell ans = o

Since cell arrays can have cell arrays as elements, the possibility for
generating an incredibly complicated data structure looms large. When
using cell arrays, keep it simple and keep it highly structured.

*N*-dimensional arrays generalize the idea of the two-dimensional array. If
the first 2 indicies correspond to the row and column position, subsequent
indices might corespond to the page on which the array appears, the book in
which the page is bound, the shelf on which the book rests, and so on.

The syntax for the *N*-dimension array is simply an extension of the
two-dimensional syntax:

and the subscripting operations that work for two dimensions extend toA(i,j,k,...)

Manipulating the dimensions of an *N*-dimensional array can be
tricky. For example, transpose (** '**) is not defined for such
arrays and so you need to use functions like

The function ` squeeze` is particularly useful for plotting
the "planes" of a 3-dimensional array (the rows and columns for each page
of the array) because

does not work because>> x = rand(6,4,3) >> plot(x(1,:,:)) % plot the "first" page ??? Error using ==> plot Data may not have more than 2 dimensions.

will "squeeze out" the the singleton dimension an thereby create a two-dimensional array that>> plot(squeeze(x(1,:,:))) % plot the "first" page

[Back to main page] [Back to language features]

Comments? Contact Jim Maneval at