Arrays and Matrices

Matlab is an abbreviation for MATrix LABoratory, it has been purely designed to work with matrices and arrays. In fact, all of the data types we saw earlier are secretly arrays.

Array creation

To create an array use the square brackets “[ ]”, to separate elements you can use either “,” or spaces. Both methods are equally valid.

a = [1, 3, 9, 8]
b = [2 4 6 4]

The above arrays have only 1 one row, they are also known as Row vectors.

To add new rows to your arrays use “;” :

c = [1 6; 4 2]

There are also ways of creating matrices using functions like zeros(Rows, Cols) or rand(Rows, Cols).

d = zeros(3,2)
e = rand(2,1)

Operations

Indexing

Matlab unlike python starts indexing at 1 instead of 0!

To index an item from an array, the row and columns should be specified. To access the element from the 2nd row, 1st column you should:

c(2,1)

If you desire to obtain an entire row or column use the : symbol. This extracts the whole 1st column of the c array.

c(:,1)
Dyson School of Design Engineering 2021 - Ivan Revenga Riesco