⇦ Back

1 Vectors

Vectors are 1-dimensional arrays of numbers (as opposed to scalars which are 0-dimensional and matrices which are 2-dimensional).

1.1 Creating

Vectors are created by using:

  • Square brackets
  • Commas or spaces between numbers (or a mixture of both)

When working with vectors the output can get quite large so we will use format compact to reduce the space used:

format compact

# Commas between numbers
x = [1, 3, 2]

# Spaces between numbers
x = [1 3 2]

# A mixture of commas and spaces
x = [1 3, 2]
## x =
##    1   3   2
## 
## x =
##    1   3   2
## 
## x =
##    1   3   2

Vectors can also be created by specifying ranges of numbers:

format compact

# Numbers between 1 and 10
x = 1 : 10

# Odd numbers between 1 and 10
x = 1 : 2 : 10
## x =
##     1    2    3    4    5    6    7    8    9   10
## 
## x =
##    1   3   5   7   9

The linspace() function can be used to create a vector of a defined length between a start and an end point with a consistent gap between successive elements:

format compact

# 8 numbers evenly spaced between 1 and 10
x = linspace(1, 10, 8)
## x =
##     1.0000    2.2857    3.5714    4.8571    6.1429    7.4286    8.7143   10.0000

1.2 Indexing

Read values in a vector by using round brackets:

format compact

# Create a 5-element vector
x = [10, 20, 30, 40, 50];

# Get the 3rd element
x(3)
## ans = 30

1.3 Describing

Use length() and size() to display details about your vector:

# Create a 5-element vector
x = [10, 20, 30, 40, 50];

length(x)
size(x)
## ans = 5
## ans =
## 
##    1   5

This tells us that our vector is 5 elements in length, with 1 row and 5 columns.

1.4 Iterating Over

You can iterate over a vector using a for loop:

# Create a 5-element vector
x = [10, 20, 30, 40, 50];

for i = 1:length(x)
    x(i)
endfor
## ans = 10
## ans = 20
## ans = 30
## ans = 40
## ans = 50

1.5 Concatenating

Add a vector onto the end of another vector using square brackets:

x1 = [10, 20, 30];
x2 = [40, 50];

x = [x1, x2]
## x =
## 
##    10   20   30   40   50

2 Matrices

Matrices are much the same as vectors except they have two dimensions.

2.1 Creating

Just like vectors, matrices are created by using square brackets together with either commas or spaces (or both) except, this time, either semi-colons or line breaks are also used as these indicate the line breaks at the end of rows:

format compact

# Create a matrix using spaces between row elements and line breaks between rows
matrix = [
    1 2
    3 4
    5 6
]

# Create a matrix using commas between row elements and semi-colons between rows
matrix = [
    1, 2;
    3, 4;
    5, 6;
]
## matrix =
##    1   2
##    3   4
##    5   6
## 
## matrix =
##    1   2
##    3   4
##    5   6

2.2 Indexing

A matrix is also indexed using round brackets, but this time two numbers are needed (corresponding to the row and column of the element you want):

# Create a 3x2 matrix
matrix = [
    1 2
    3 4
    5 6
];

# Index the matrix
matrix(2, 2)
## ans = 4

More advanced indexing is also possible, using vectors to define the rows and columns you want:

# Create a 4x4 matrix
matrix = [
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
];

# Index the matrix
matrix(1 : 2 : 3,  2 : 2 : 4)
## ans =
## 
##     2    4
##    10   12

This has given us the odd-numbered rows (1 and 3) and the even-numbered (2 and 4) columns.

A colon : represents ‘all’ rows or columns:

# Create a 4x4 matrix
matrix = [
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
];

# Index the matrix
matrix(1 : 2 : 3, :)
## ans =
## 
##     1    2    3    4
##     9   10   11   12

This has given us the odd-numbered rows (1 and 3) and all columns.

2.3 Describing

The length(), size(), rows() and columns() functions all work for describing a matrix:

# Create a 3x2 matrix
matrix = [
    1 2
    3 4
    5 6
];

length(matrix)
size(matrix)
rows(matrix)
columns(matrix)
## ans = 3
## ans =
## 
##    3   2
## 
## ans = 3
## ans = 2

2.4 Iterating Over

You can iterate over a matrix using two for loops:

# Create a 3x2 matrix
matrix = [
    1 2
    3 4
    5 6
];

for i = 1:rows(matrix)
    for j = 1:columns(matrix)
        matrix(i, j)
    endfor
endfor
## ans = 1
## ans = 2
## ans = 3
## ans = 4
## ans = 5
## ans = 6

2.5 Finding

To find a particular value in a matrix, use == to find which elements are equal to what you are looking for:

matrix = [
    1 2
    3 3
    3 6
    7 8
];

matrix == 3
## ans =
## 
##   0  0
##   1  1
##   1  0
##   0  0

Then, use find() to get the indices of these locations (index numbers start at the top-left and increase going down the columns first, then along the rows):

matrix = [
    1 2
    3 3
    3 6
    7 8
];

find(matrix == 3)
## ans =
## 
##    2
##    3
##    6

So the number 3 is the second, third and sixth element of this matrix, counting from the top-left downwards and then to the right.

If you search for a two-element vector in a two-column matrix, Octave will search each column separately:

matrix = [
    1 2
    3 3
    3 6
    7 8
];

find(matrix == [3, 8])
## ans =
## 
##    2
##    3
##    8

The first column was searched for the number 3 and it was found at indices 2 and 3. The second column was searched for the number 8 and it was found at index 8.

Alternatively, you can search only a subset of the matrix:

matrix = [
    1 2
    3 3
    3 6
    7 8
];

find(matrix(:, 1) == 3)
## ans =
## 
##    2
##    3

All rows and the first column of the matrix was searched for the number 3, and it was found at indices 2 and 3.

3 Array Operations

3.1 Scalar Mathematics

Addition, subtraction, multiplication and division of an array by a scalar simply applies the operation to all the elements in the array:

format compact

matrix = [
    1 2
    3 4
    5 6
];

% Scalar addition
matrix + 3

% Scalar subtraction
matrix - 3

% Scalar multiplication
matrix * 3

% Scalar division
matrix / 3
## ans =
##    4   5
##    6   7
##    8   9
## 
## ans =
##   -2  -1
##    0   1
##    2   3
## 
## ans =
##     3    6
##     9   12
##    15   18
## 
## ans =
##    0.3333   0.6667
##    1.0000   1.3333
##    1.6667   2.0000

3.2 Element-Wise Multiplication

If you want to perform an operation between every element in one array and the corresponding elements in a second array, you can add a full stop before the operation:

format compact

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

A .* B
## ans =
##     1    4
##     9   16

This causes each element in A to be multiplied by the corresponding element in B. This is not the same as matrix multiplication (dot products and/or cross products). This element-wise mode of operation also applies to division (./) and exponentiation (.^).

3.3 Dot Products

Given an \(r \times n\) matrix A and an \(n \times c\) matrix B the dot product will produce an \(r \times c\) matrix:

format compact

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

A * B
## ans =
##    32   37   17
##    16   15   19
##    26   27   23

A \(3 \times 2\) matrix times a \(2 \times 3\) matrix produces a \(3 \times 3\) matrix.

3.4 Inverse

The inverse of A is inv(A) or A^-1:

format compact

A = [
    0 2 1
    -1 -2 0
    -1 1 2
];

inv(A)
A^-1
## ans =
##   -4.0000  -3.0000   2.0000
##    2.0000   1.0000  -1.0000
##   -3.0000  -2.0000   2.0000
## 
## ans =
##   -4.0000  -3.0000   2.0000
##    2.0000   1.0000  -1.0000
##   -3.0000  -2.0000   2.0000

Use this to solve for unknown variables in a system on linear equations: Ax = b

format compact

A = [
    3 2 -1
    2 -2 4
    -1 0.5 -1
];
b = [
    1
    -2
    0
];

x = inv(A) * b
x = A^-1 * b
x = A \ b
## x =
##    1.0000
##   -2.0000
##   -2.0000
## 
## x =
##    1.0000
##   -2.0000
##   -2.0000
## 
## x =
##    1.0000
##   -2.0000
##   -2.0000

⇦ Back