MATLAB Programming/Arrays

From Wikibooks, open books for an open world
< MATLAB Programming
Jump to: navigation, search



Introduction to Arrays[edit]

Arrays are the fundamental data type of MATLAB. Indeed, the former data types presented here, strings and number, are particular cases of arrays. As in many traditional languages, arrays in MATLAB are a collection of several values of the same type (by default, the type is equivalent to the C type double on the same architecture. On x86 and powerpc, it is a floating point value of 64 bits). They are indexed through the use of a single integer or, to get more than one value, an array of integers..

Declaring Arrays[edit]

Row and Column Arrays[edit]

A simple way to create a row array is to give a comma separated list of values inside brackets:

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

The commas can be omitted for a row array because MATLAB will assume you want a row array if you don't give it any separators. However, the commas make it easier to read and can help with larger arrays. The commas indicate that the array will be a horizontal array.

To make a column array you can use semicolons to separate the values.

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

All elements of an array must be the same data type, so for example you cannot put a function handle and a double in the same array.

Declaring multi-dimensional arrays[edit]

Arrays can be multi-dimensional. To create a 2 dimensional array (a matrix in Linear Algebra terms), we have to give a list of comma separated values, and each row should be separated by a semi colon:

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

In MATLAB the term array is synonymous with matrix and will more often than not be referred to as a matrix. It should be noted that a matrix, as its mathematical equivalent, requires all its rows and all its columns to be the same size:

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

Properties of MATLAB arrays and matrices[edit]

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]

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.