Page 2 of 3
Arithmetic with Matrices
The important thing about matrices in Octave is that the arithmetic operators work as you would expect from math rather than programming.
As long as the matrices and scalars involved in the expression are conformable then the operation will be a matrix operation.
will multiply the two matrices together.
If C=10 then
is a scalar multiplication of each element of A by 10.
The only variation in the way matrices are treated is that you can opt for an element-by-element operation by putting a dot before the operator.
performs an element-by-element multiplication of the two matrices and not a matrix multiplication i.e. aijbij.
There are two specifically matrix operations that we need to know about. The single quote performs a complex transpose, e.g.
is a column vector. For a real matrix the dash is a simple transpose. If the matrix is complex the dash also takes the complex conjugate (i.e. it is the Hermitian transpose). If you want a simple transpose of a complex matrix then use dot single quote.
The second is the inverse which is more complicated. You can use the inverse function to find the inverse of any square non-singular matrix. For example
displays the identity matrix.
There is another way to use the inverse via an operator.
The expression x\y is the left division of y by x and is equivalent to
The advantage of using this notation is that the inverse isn't actually used in the calculation.
The expression x/y is the right division of x by y and it is equivalent to
Again the inverse matrix is never computed and generalized inverses are used if necessary.
In an ideal world we would just define some matrices and get on with combining them using matrix arithmetic. In practice matrices are often the wrong shape for the job and we need to get at sub-matrices or even single elements.
This is what indexing is all about - specifying sub-matrices.
The indexing operator is () and if you specify index values for each dimension of a matrix then things work as you would expect . For example
is the value in row 1 column 2.
You can assign a new value to a single element e.g.
If you supply just one index for a 2D matrix then it is treated as a single 1D column vector obtained by stacking up each column of the original matrix.
To define sub-matrices you need to use more complicated indexing. There are two approaches - vectors of simple indexes or ranges.
A vector of indexes just picks out the combined set of elements that each index would pick out. For example:
picks out A(1,1) and A(2,1) and the result is a column vector because you have specified part of a column of the original matrix.
It doesn't matter if the vector of indexes is a row or column vector. That is A([1;2],1) is the same as A([1,2],1).
You can use vector indexes in both index positions. for example:
picks out A(1,1), A(2,1), A(1,2) and A(2,2) which is returned as a 2x2 matrix because this is the "shape" of the sub matrix in the original array.
The most important thing to notice about vector indexes is that they allow you to pick out non-contiguous columns and rows. For example:
also picks out a 2x2 matrix but it takes the intersection of the first and third rows and the first and second columns.
In general if you write something like:
Matrix[ v1, v2]
where v1 and v2 are vectors then this gives a matrix made up of the rows specified by v1 and the columns specified by v2.
You can use variables within vector indexing, for example:
works and is the same as:
Notice that any sub-matrix you specify can be retrieved or you can assign a matrix of the same size to it. For example:
zeros the intersection of the first and third rows and the first and second columns.
Vector indexes work well when you want to select a small number of rows and columns but they are a lot of work if the number increases. For example how would you select the first 1000 elements of column 1? Clearly
isn't going to be easy typing.
The solution is to use a range. A range represents a numerical range of value. For example, 1:10 generates the numbers 1 to 10. You can also specify a step size, so for example 1:2:7 (or 1:2:8) generates 1,3,5,7. Using a range you can pick out lots of rows and columns very easily but only if they have a simple numerical pattern.
For example to pick out the first 1000 elements in column 1 you would write:
You can write
and get the same 1000 rows but a range is interpreted as a row vector so you don't have to.
If you would like every other row of the matrix you can use:
In general a range is specified as
and if you leave out the increment it is assumed to be 1 and the range is
The increment can be negative.
You can also specify a default range as just a : which means the entire possible range. So
means all the rows and column 1 i.e. all of column 1 and
means all of the columns and row 1 and finally
is the same as A.
You can also use A(:) to mean all of the rows and columns returned as a single column vector in column order.
As with vector indexes you can use variables to specify the start, stop and increment values.
You can also assign to the selected sub-matrix if what you are assigning has the same size as the sub-matrix.
Finally you can mix vector and indexing.
If you want to pick out the first 1000 rows and the 1500th row you can write:
Also notice that an index can occur more than once in both vector, range and mixed indexing. If this happens the column or row is included more than once. This can be used to construct larger matrices from smaller ones - more on this in the next section. For example:
makes C a 3x4 matrix with identical rows.