Often you need to define regular matrices, i.e. that have a regular pattern of entries. For example:
sets up a 3x3 identity matrix usually represented by I hence the name fo the function. In general eye(m.n) is a unit diagonal m x n matrix.
You can set up a matrix of 1s using ones(n) or ones(m,n) for a n x n or m x n matrix. There is also zeros that works in the same way.
There are various other functions that will create other constant, periodic or well known matrices. These you can look up in the documentation and they are not difficult to use.
There is one other general idiom used a lot in Octave to build larger matrices from smaller. You can build a bigger matrix by putting together, i.e. concatenating, larger matrices. For this to worth the sub-matrices have to be the right size to fit together to make a matrix without any holes or ragged edges.
For example if
is [1,2,3,4] and
is [1,2;3,4] i.e. a 2x2 matrix.
This can become confusing but as long as you keep a clear head and think of it as fitting a jigsaw together it all works.
For example if C is the 2x2 matrix given above then:
is a 4x4 square matrix equal to
There aren't many places that Octave differs from other languages but its treatment of functions is worth noting.
At its simplest a function is defined using;
function name Octave commands endfunction
You can define a function interactively at the command prompt or in a file of the same name. You can include multiple functions within a file but if you want to load the function by using its name the file and the function have to have the same name.
If you want to include parameters you can in the usual way. It is the way return values are specified that makes Octave slightly different from the norm. In most languages you finish a function with a return value statement which not only brings the function to a close but returns the value specified. This is not how Octave works.
In Octave you specify the return variables at the start of the function. That is
function variable= name(parameters) Octave commands endfunction
When the function finishes any value stored in the specified variable is returned as the result of the function.
You can also exit a function using the return command but you don't have to specify the return value as this is simply the value in the return variable.
function myResult=myFunction(myParameter) myResult=myParameter endfunction
With this definition the command
stores 1 in value because myParameter is set to 1 and the function sets myResult to whatever myParameter is and this is the variable that has the return value.
A function to add two numbers or matrices would be:
function c=add(a,b) c=a+b; endfunction
You could save this in a file called add.m and use the function as
and of course answer would contain 3.
You can specify multiple return variables as a row vector.
For example if you want a function that returns the sum and difference of two value you could write:
function [s,d] =sumdiff(a,b) s=a+b; d=a-b; endfunction
To use this you can either assign the result to variables in a row vector e.g.
stores 3 in x and -1 in y.
There are some utility functions that modify the way multiple return values work but they are easy to work out once you have seen the basic idea.
All variables in functions are local to the function and if you don't assign a value to a return variable you will get an error message.
In most languages for loops are the way to get iterative work done. Octave has for loops, if statements and everything else you would expect to find in a programming language but they tend not to be used as much. The reason is that the predominant mode of programming is to use matrix/vector operations.
Functions are automatically applied to entire vectors or matrices element-by-element. For example:
returns the a vector equal to [sin(1),sin(2),sin(3)] - this would normally need a for loop.
Even user defined functions work in this way without you having to do anything extra.
If a function has two or more parameters then the operation is still performed on the elements taken a set at a time.
For example even the sumdiff function we defined earlier will work with vectors:
and you will discover that x is [1+4,2+5,3+6] and y is [1-4,2-5,3-6] .
If you try this with different sized vectors you will discover that the elements of the smaller are reused until the computation is complete - look up "broadcasting" in the documentation.
Finally the most difficult vectorization for general programmers to absorb is the inner product.
If you want to work out the sum of xiyi i.e the sum of the elementwise product of two vectors, you don't need to use a loop. Instead you simply convert to an inner product form -
This transposes x and then multiplies the two together - which in matrix terms is the sum of the elementwise product of the row by the column. Notice that this is not the same as x'*y which is a matrix called the outer product.
You can even use the inner product to sum a vector. For example:
x=[1,2,3]; y=ones(1,3); s=x*y';
This works because y is set to a row vector of 1s and thus we simply sum up the elements of y.
Again no for loops were harmed in this calculation.
Why do we bother - simply because vectorized forms are faster to work out and fit more naturally with the math.
There is a lot of goodness in Octave but this article has covered most of the ideas that a general programmer would find difficult at first.
You need to find out about the control instructions - for loop, if, etc. You also need to find out about strings and other data structures. Octave also has excellent plotting commands and lots of libraries that will simplify your math tasks.
Now that you have a start on programming in Octave, this is, in the main, just a matter of reading the documentation.