

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. Zeros the intersection of the first and third rows and the first and second columns. Notice that any sub-matrix you specify can be retrieved or you can assign a matrix of the same size to it. You can use variables within vector indexing, for example: 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. For example:Īlso picks out a 2x2 matrix but it takes the intersection of the first and third rows and the first and second columns. The most important thing to notice about vector indexes is that they allow you to pick out non-contiguous columns and rows. 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. You can use vector indexes in both index positions.

It doesn't matter if the vector of indexes is a row or column vector. 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. Vector IndexesĪ vector of indexes just picks out the combined set of elements that each index would pick out. There are two approaches - vectors of simple indexes or ranges. To define sub-matrices you need to use more complicated indexing. 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. You can assign a new value to a single element e.g. The indexing operator is () and if you specify index values for each dimension of a matrix then things work as you would expect. This is what indexing is all about - specifying sub-matrices. In practice matrices are often the wrong shape for the job and we need to get at sub-matrices or even single elements. In an ideal world we would just define some matrices and get on with combining them using matrix arithmetic. The expression x/y is the right division of x by y and it is equivalent toĪgain the inverse matrix is never computed and generalized inverses are used if necessary. The advantage of using this notation is that the inverse isn't actually used in the calculation. The expression x\y is the left division of y by x and is equivalent to There is another way to use the inverse via an operator. You can use the inverse function to find the inverse of any square non-singular matrix. The second is the inverse which is more complicated. If you want a simple transpose of a complex matrix then use dot single quote. If the matrix is complex the dash also takes the complex conjugate (i.e. The single quote performs a complex transpose, e.g.įor a real matrix the dash is a simple transpose. There are two specifically matrix operations that we need to know about. Performs an element-by-element multiplication of the two matrices and not a matrix multiplication i.e. 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. Is a scalar multiplication of each element of A by 10. The important thing about matrices in Octave is that the arithmetic operators work as you would expect from math rather than programming.Īs long as the matrices and scalars involved in the expression are conformable then the operation will be a matrix operation. Matrices cannot be irregular - if the first row has three values then the subsequent rows must have three values. When typing in a matrix the comma separator means move on one column and the semicolon means move on one row. Working with multidimensional structures is possible but more complicated. What makes Octave special is the ease with which you can create matrices and work with them.Ī matrix is define using square brackets to contain a list of numbers. There are also strings that can be stored in variables and manipulated, and these work in the way you would expect. Logical values are represented by 1 as true and 0 as false. There is also a special missing data value, NA, which can be used to implement statistical procedures that recognize missing data. There are built-in functions for working with real and complex values. Note: i has to trail the value in a complex number - 1+2i, not 1+i2, and there can be no spaces between the i and the number.Īll values in Octave are represented internally as double precision value (including integers). Complex values use i or j for the imaginary component. Numbers can be integer, floating point and complex. Variables are not typed and you can store anything in a variable at any time as long as it makes sense. All data in Octave is a matrix even a single number is a 1x1 matrix. The central data structure in Octave is the matrix and the sooner we get to grips with it the better.
