Often you need to define regular matrices, i.e. that have a regular pattern of entries. For example:

A=eye(3)

sets up a 3x3 identity matrix usually represented by I hence the name of 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

A=[1,2] B=[3,4]

then

C=[A,B]

is [1,2,3,4] and

C=[A;B]

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:

D=[C,C;C,C]

is a 4x4 square matrix equal to

[1,2,1,2;3,4,3,4;1,2,1,2;3,4,3,4]

Functions

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.

For example:

function myResult=myFunction(myParameter) myResult=myParameter endfunction

With this definition the command

value=myFunction(1)

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

answer=add(1,2);

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.

[x,y]=sumdiff(1,2)

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.

Vectorization

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:

sin([1,2,3])

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:

[x,y]=sumdiff([1,2,3],[4,5,6])

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 x_{i }y_{i} 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 -

result=x*y'

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.

Where next?

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. In particular the optimization functions are extremely general and make it possible to write programs that implement things like neural networks using custom measures of goodness of fit.

It is worth mentioning that most Octave programs make minimal use of loops and this is perhaps the one thing that that is difficult for a programmer familiar with another language to master. Writing an Octave program is more like writing a mathematical expression within some code to get data and present results.

Now that you have a start on programming in Octave, this is, in the main, just a matter of reading the documentation and always remember that Octave and MatLab are more or less identical.