Octave:Vectors and matrices
From AIMSWiki
| Table of contents |
Creating vectors and matrices
Here is how we specify a row vector in Octave:
octave:1> x = [1, 3, 2] x = 1 3 2
Note that
- the vector is enclosed in square brackets;
- each entry is separated by a comma
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
You can use the standard operators to
- add (
+), - subtract (
-), and - 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 (.') operator.)
- The power operator (
^) can also be used to compute real powers of square matrices.
Useful Functions
- sum()
- mean()
- max(), min()
- size(), length()
- find()
Element operations
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 (.^).
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
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
Ranges
We can also select a range of rows or columns from a matrix. We specify a range with
start:step:stop
The first number in the range is start, the second is start+step, the third, start+2*step, etc. The last number is less than or equal to stop.
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
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. To index the last 3 elements of the vector x, use
octave:5> x(end-2:end) ans = 7.6000 3.0000 8.0000
Functions
The following functions can be used to create and manipulate matrices.
Creating matrices
-
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.
-
zeros(m, n)returns an
matrix filled with 0s.
-
rand(m, n)returns an
matrix filled with random elements drawn uniformly from [0,1).
-
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(x)ordiag(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.
-
logspace(a, b, n)returns a vector with n values, such that the first element equals 10a, the last element equals 10b and the ratio between consecutive elements is constant. The last argument, n is optional with default value 50.
Other matrices
There are some more functions for creating special matrices. These are
hankel(Hankel matrix),hilb(Hilbert matrix),invhilb(Inverse of a Hilbert matrix),sylvester_matrix(Sylvester matrix),toeplitz(Toeplitz matrix),vander(Vandermonde matrix).
Use help to find out more about how to use these functions.
Changing matrices
-
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, n, is optional with default value 1.
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 mn. 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
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 index

