Octave Programming Tutorial/Vectors and matrices
Creating vectors and matrices[edit | edit source]
Here is how we specify a row vector in Octave:
octave:1> x = [1, 3, 2] x = 1 3 2
- the vector is enclosed in square brackets;
- each entry is separated by an optional comma.
x = [1 3 2]results in the same row vector.
To specify a column vector, we simply replace the commas with semicolons:
octave:2> x = [1; 3; 2] x = 1 3 2
From this you can see that we use a comma to go to the next column of a vector (or matrix) and a semicolon to go to the next row. So, to specify a matrix, type in the rows (separating each entry with a comma) and use a semicolon to go to the next row.
octave:3> A = [1, 1, 2; 3, 5, 8; 13, 21, 34] A = 1 1 2 3 5 8 13 21 34
Operators[edit | edit source]
You can use the standard operators to
- add (
- subtract (
- multiply (
matrices, vectors and scalars with one another. Note that the matrices need to have matching dimensions (inner dimensions in the case of multiplication) for these operators to work.
- The transpose operator is the single quote:
'. To continue from the example in the previous section,
octave:4> A' ans = 1 3 13 1 5 21 2 8 34
(Note: this is actually the complex conjugate transpose operator, but for real matrices this is the same as the transpose. To compute the transpose of a complex matrix, use the dot transpose (
- The power operator (
^) can also be used to compute real powers of square matrices.
Element operations[edit | edit source]
When you have two matrices of the same size, you can perform element by element operations on them. For example, the following divides each element of A by the corresponding element in B:
octave:1> A = [1, 6, 3; 2, 7, 4] A = 1 6 3 2 7 4 octave:2> B = [2, 7, 2; 7, 3, 9] B = 2 7 2 7 3 9 octave:3> A ./ B ans = 0.50000 0.85714 1.50000 0.28571 2.33333 0.44444
Note that you use the dot divide (
./) operator to perform element by element division. There are similar operators for multiplication (
.*) and exponentiation (
Let's introduce a scalar for future use.
a = 5;
The dot divide operators can also be used together with scalars in the following manner.
C = a ./ B
returns a matrix, C where each entry is defined by
i.e. a is divided by each entry in B. Similarly
C = a .^ B
return a matrix with
Indexing[edit | edit source]
You can work with parts of matrices and vectors by indexing into them. You use a vector of integers to tell Octave which elements of a vector or matrix to use. For example, we create a vector
octave:1> x = [1.2, 5, 7.6, 3, 8] x = 1.2000 5.0000 7.6000 3.0000 8.0000
Now, to see the second element of x, type
octave:2> x(2) ans = 5
You can also view a list of elements as follows.
octave:3> x([1, 3, 4]) ans = 1.2000 7.6000 3.0000
This last command displays the 1st, 3rd and 4th elements of the vector x.
To select rows and columns from a matrix, we use the same principle. Let's define a matrix
octave:4> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = 1 2 3 4 5 6 7 8 9
and select the 1st and 3rd rows and 2nd and 3rd columns:
octave:5> A([1, 3], [2, 3]) ans = 2 3 8 9
The colon operator (
:) can be used to select all rows or columns from a matrix. So, to select all the elements from the 2nd row, type
octave:6> A(2, :) ans = 4 5 6
You can also use
: like this to select all Matrix elements:
octave:7> A(:,:) ans = 1 2 3 4 5 6 7 8 9
Ranges[edit | edit source]
We can also select a range of rows or columns from a matrix. We specify a range with
You can actually type ranges at the Octave prompt to see what the results are. For example,
octave:3> 1:3:10 ans = 1 4 7 10
The first number displayed was
start, the second was
start + step, the third,
start + (2 * step). And the last number was less than or equal to
Often, you simply want the step size to be 1. In this case, you can leave out the step parameter and type
octave:4> 1:10 ans = 1 2 3 4 5 6 7 8 9 10
As you can see, the result of a range command is simply a vector of integers. We can now use this to index into a vector or matrix. To select the submatrix at the top left of A, use
octave:4> A(1:2, 1:2) ans = 1 2 4 5
Finally, there is a keyword called
end that can be used when indexing into a matrix or vector. It refers to the last element in the row or column. For example, to see the last column in a Matrix, you can use
octave:5> A(:,end) ans = 3 6 9
Functions[edit | edit source]
The following functions can be used to create and manipulate matrices.
Creating matrices[edit | edit source]
tril(A)returns the lower triangular part of A.
triu(A)returns the upper triangular part of A.
eye(n)returns the identity matrix. You can also use
eye(m, n)to return rectangular identity matrices.
ones(m, n)returns an matrix filled with 1s. Similarly,
ones(n)returns square matrix.
zeros(m, n)returns an matrix filled with 0s. Similarly,
zeros(n)returns square matrix.
rand(m, n)returns an matrix filled with random elements drawn uniformly from . Similarly,
rand(n)returns square matrix.
randn(m, n)returns an matrix filled with normally distributed random elements.
randperm(n)returns a row vector containing a random permutation of the numbers .
diag(A). For a vector, x, this returns a square matrix with the elements of x on the diagonal and 0s everywhere else. For a matrix, A, this returns a vector containing the diagonal elements of A. For example,
octave:16> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = 1 2 3 4 5 6 7 8 9 octave:17> x = diag(A) ans = 1 5 9 octave:18> diag(x) ans = 1 0 0 0 5 0 0 0 9
linspace(a, b, n)returns a vector with n values, such that the first element equals a, the last element equals b and the difference between consecutive elements is constant. The last argument, n, is optional with default value 100.
octave:186> linspace(2, 4, 2) ans = 2 4 octave:187> linspace(2, 4, 4) ans = 2.0000 2.6667 3.3333 4.0000 octave:188> linspace(2, 4, 6) ans = 2.0000 2.4000 2.8000 3.2000 3.6000 4.0000
logspace(a, b, n)returns a vector with n values, such that the first element equals , the last element equals and the ratio between consecutive elements is constant. The last argument, n is optional with default value 50.
octave:189> logspace(2, 4, 2) ans = 100 10000 octave:190> logspace(2, 4, 4) ans = 1.0000e+02 4.6416e+02 2.1544e+03 1.0000e+04 octave:191> logspace(2, 4, 5) ans = 1.0000e+02 3.1623e+02 1.0000e+03 3.1623e+03 1.0000e+04
Other matrices[edit | edit source]
There are some more functions for creating special matrices. These are
invhilb(Inverse of a Hilbert matrix),
sylvester_matrix(Sylvester matrix) - In v3.8.1 there is a warning: sylvester_matrix is obsolete and will be removed from a future version of Octave; please use hadamard(2^k) instead,
help to find out more about how to use these functions.
Changing matrices[edit | edit source]
fliplr(A)returns a copy of matrix A with the order of the columns reversed, for example,
octave:49> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 octave:50> fliplr(A) ans = 4 3 2 1 8 7 6 5 12 11 10 9
flipud(A)returns a copy of matrix A with the order of the rows reversed, for example,
octave:51> flipud(A) ans = 9 10 11 12 5 6 7 8 1 2 3 4
rot90(A, n)returns a copy of matrix A that has been rotated by (90n)° counterclockwise. The second argument, , is optional with default value 1, and it may be negative.
octave:52> rot90(A) ans = 4 8 12 3 7 11 2 6 10 1 5 9
reshape(A, m, n)creates an matrix with elements taken from A. The number of elements in A has to be equal to . The elements are taken from A in column major order, meaning that values in the first column () are read first, then the second column (), etc.
octave:53> reshape(A, 2, 6) ans = 1 9 6 3 11 8 5 2 10 7 4 12
sort(x)returns a copy of the vector x with the elements sorted in increasing order.
octave:54> x = rand(1, 6) x = 0.25500 0.33525 0.26586 0.92658 0.68799 0.69682 octave:55> sort(x) ans = 0.25500 0.26586 0.33525 0.68799 0.69682 0.92658
Linear algebra[edit | edit source]
For a description of more operators and functions that can be used to manipulate vectors and matrices, find eigenvalues, etc., see the Linear algebra section.
Return to the Octave Programming Tutorial index