Arrays

Arrays are the basic method (along with vectors) of storing data in Matlab.  In order to create arrays specify the values of each cell in the matrix.

For example:

A = [1 1 1; 2 3 4; 5 5 5]

will create a 3×3 matrix.  The notation is very important when assigning values to an array.  Include spaces between numbers to demarcate columns and semicolons (;) to demarcate rows.  Ensure that the number of columns in each row are equal).

Once you have predefined the array (A) you can access certain elements in this array with the following:

A(row,column)

For example:

A(1,2)

Will access the value in the 1st row, 2nd column and return ans = 1.

A(:,2)

Will access  the 2nd column and return ans = [ 1 ; 3 ; 5 ].

A(1,:)

Will access the first row and return ans = [ 1 1 1 ].

A(1,1:2)

Will access the first row and columns 1 thru 2, returning ans = [1 1; 2 3; 5 5].

A(:,[3 1 2])

Will access the columns in the order specified, returning ans = [1 1 1; 4 2 3; 5 5 5].

A.'

Will compute the transpose of the matrix, returning ans = [1 2 5; 1 3 5; 1 4 5].

A(:)

Will return A as a column vector, returning ans = [1; 2; 5; 1; 3; 5; 1; 4; 5].

A(:).'

Will return A as a row vector, returning ans = [1, 2, 5, 1, 3, 5, 1, 4, 5].

Cell Arrays

A cell array allows you to save any type of data into a matrix.  Unlike regular arrays which utilize parantheses, ( ), in order to access the elements, cell arrays will use the curly brackets, { }.

For example:

A = cell(2)

will create a 2×2 matrix of arrays.  Assignments can be made to each individual matrix as follows:

A{1,1} = [1 1 1; 2 3 4; 5 5 5]
A{1,2} = 'Hello';
A{2,1} = pi();
A{2,2} = cell(3);

As you can see scalars, vectors, strings and even additional cell arrays can be nested within a single cell array A.  Try to see what is returned when you enter A{1,2}(2) or any other combination!