## Arrays In MATLAB

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]

### Numeric (or double) arrays

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 MATrix LABoratory), 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:

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 Ajk 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 A23 has the value 5. In MATLAB notation, the j-kth element of an array named A is denoted A(j,k). In the example above, the same element is identified as A(2,3).

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

Getting information on arrays

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

MATLAB functionwhat it provides
[M,N] = size(A)extent (dimensions) of the array
L = length(A)max(size(A)) - use with vectors only
s = whosextents 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 whos is used without an output and so displays information to the command window. In this second mode, the information on the workspace is not available for further use (in a script or function, for example).

Keep it rectangular

All numeric arrays must be rectangular which means two things:

1. All rows in the array must have the same number of columns
2. 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, cos, log, besselj, etc.) apply element-by-element. For example, applying log to an array

```   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 A:

```   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 column-oriented functions because, by default, they apply separately to each column of an array and in doing so, generate a vector rather rather a 2D array. Such functions are ususally associated with data-analysis. The primary functions that act in this manner are

```           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.

[TOPICS]

### Cell arrays

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 C) is referred to (or addressed) by the traditional indexing procedures:

```   >> cell1 = C(1)    % "The 1,1 cell of C"
cell1 =
[2x2 double]
```
However, the variable cell1 is not a 2D numeric array as might be supposed from the creation of C. Rather, cell1 is itself a cell array - a 1-by-1 cell array, as can be seen by either the whos or class commands:
```   >> 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
```
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 cell11 leads to a syntax error:
```   >> inv(cell1)
??? Function 'inv' not defined for variables of class 'cell'.
```
To actually get at the contents of a cell, you need to use indices with the braces. Thus, to get the numeric array "stored" in the first cell of C, you would use the syntax
```   >> arr = C{1};    % "The contents of the first cell"
>> class(arr)
ans =
double
>> inv(arr)
ans =
-2.0000    1.0000
1.5000   -0.5000
```
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:

### Important distinctions in cell-array addressing

Indexing methodWhat 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.

[TOPICS]

### N-dimensional arrays

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:

```  A(i,j,k,...)
```
and the subscripting operations that work for two dimensions extend to N dimensions. Additionally, functions like size will provide more than just 2 values.

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 shiftdim, permute and squeeze.

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 plot does not accept any array with a dimension above 2. For example,

```   >> x = rand(6,4,3)
>> plot(x(1,:,:))             % plot the "first" page
??? Error using ==> plot
Data may not have more than 2 dimensions.
```
does not work because size(x(1,:,:) indicates that the object is a 3-dimensional array (try it). Using squeeze,
```   >> plot(squeeze(x(1,:,:)))             % plot the "first" page
```
will "squeeze out" the the singleton dimension an thereby create a two-dimensional array that plot can handle.

[TOPICS]

[Back to main page] [Back to language features]
Comments? Contact Jim Maneval at maneval@bucknell.edu