# MATLAB Programming/Arrays

## Introduction to Arrays[edit | edit source]

An array is the most fundamental data type in MATLAB. In MATLAB, as in many traditional languages, arrays are a collection of several values of the same type. The string and number data type formerly presented are particular cases of arrays.

A * matrix* is an array with two dimensions. Most arrays have the same data type; however, a

*is an array with varying data types. If no data type is specified for numbers, then the default data type is the equivalent to the double precision floating point in the C programming language on the same architecture. For example on x86 and powerpc a double has 64 bits.*

**cell array**The following list are pages involving work with MATLAB arrays:

## Declaring Arrays[edit | edit source]

### Row and Column Arrays[edit | edit source]

A row array is created using comma separated values inside brackets:

>> array = [0, 1, 4, 5] array = 0 1 4 5

Sometimes commas are omitted for simple row arrays. Omitting commas is not recommended because in larger more complex arrays white-spaces can be ambiguous. Commas almost always indicate an array is horizontal.

A column array is created using semicolons to separate values:

>> column = [1; 2; 3] column = 1 2 3

### Declaring multi-dimensional arrays[edit | edit source]

A two dimensional array (or a matrix) is declared with commas separating columns, and semicolons separating rows:

>> matrix = [1, 2, 3; 4, 5, 6] matrix = 1 2 3 4 5 6

Simple matrix manipulation is the basis of many linear algebra computations. To use arrays of more than two dimensions, a matrix has to be extended using indexing.

When declaring arrays in MATLAB all rows and all columns need have same size. If not an error message will be generated:

>> matrix = [1, 2, 3; 4, 5] ??? Error using ==> vertcat All rows in the bracketed expression must have the same number of columns.

## Indexing Arrays[edit | edit source]

Arrays are indexed using integers. To return a single element in a simple array, use a single integer.

>> array = [0, 1, 4, 5]; >> array(3) ans = 4

To return a single element in a two dimensional array one number as a row index and the second as a column index.

>> matrix = [1, 2, 3; 4, 5, 6]; >> matrix(2,2) ans = 5

To return multiple elements in an array an array can be used as an index.

>> array = [0, 1, 4, 5]; >> array([1 3]) ans = 0 4

To return the last element of an array use (end).

>> array = [0, 1, 4, 5]; >> array(end) ans = 5

A range of indexes can be returned using a colon(:)

>> array = [0, 1, 4, 5]; >> array(2:end) ans = 1 4 5

## Properties of MATLAB arrays and matrices[edit | edit source]

Contrary to low level languages such as C, an array in MATLAB is a more high level type of data: it contains various information about its size, its data type, and so on.

>> array = [0,1,4,5]; >> length(array) ans = 4 >> class(array) ans = double

The number of rows and columns of the matrix can be known through the built-in size function. Following the standard mathematical convention, the first number is the number of rows and the second is the number of columns:

>> matrix = [1, 2, 3; 4, 5, 6]; >> size(matrix) ans = 2 3

The goal of MATLAB arrays is to have a type similar to mathematical vectors and matrices. As such, row and column arrays are not equivalent. Mono-dimensional arrays are actually a special case of multi-dimensional arrays, and the 'size' function can be used for them as well.

>> size(array) ans = 1 4

Row and column do not have the same size, so they are not equivalent:

>> size(column) ans = 3 1 >> size(row) ans = 1 3

## Why Use Arrays?[edit | edit source]

A major advantage of using arrays and matrices is that it lets you avoid using loops to perform the same operation on multiple elements of the array. For example, suppose you wanted to add 3 to each element of the array [1,2,3]. If MATLAB didn't use arrays you would have to do this using a FOR loop:

>> array = [1,2,3]; >> for ii = 1:3 array(ii) = array(ii) + 3; >> end >> array array = [4,5,6]

Doing this is *not efficient* in MATLAB, and it will make your programs run very slowly. Instead, you can create another array of 3s and add the two arrays directly. MATLAB automatically separates the elements:

>> array = [1,2,3]; >> arrayofthrees = [3,3,3]; >> array = array + arrayofthrees array = [4,5,6];

If all you are doing is adding a constant, you can also omit the declaration of 'arrayofthrees', as MATLAB will assume that the constant will be added to all elements of the array. This is very useful, for example if you use an array with variable size:

>> array = [1,2,3]; >> array + 3 ans = [4,5,6]

The same rule applies to scalar multiplication.

See Introduction to array operations for more information on the operations MATLAB can perform on arrays.

Arrays are a fundamental principle of MATLAB, and almost everything in MATLAB is done through a massive use of arrays. To have a deeper explanation of arrays and their operations, see Arrays and matrices.