Page 1 of 3 Octave is an open source language that makes doing difficult math easy. It supports matrix operations and has lots of different types of builtin mathematical operations. It isn't as well known as it deserves to be, and if you are a programmer it can be difficult to find out what you need to know. Hence a programmer's guide.
This guide gives you the essence of Octave. It is designed to answer the questions that arise when you first encounter the language with some knowledge of any other language no matter how slight. It isn't an in depth account and there are lots of topics that aren't even mentioned in passing. The aim is to get you started and to provide an orientation that will make exploring the rest of Octave easier.
Octave is an interpreted language that is pragmatic rather than theoretically pure. It gets the job done but there aren't many interesting language features that are going to make you think hard about programming. However, if you have some math you want to get done, Octave will enable you to solve the problem fast. You can also think of it as the math companion of the R statistical language. Octave is ideal for general math or when you need to implement some general statistical algorithm and play with it.
Getting Octave
If you are running Linux or OSX then you will find binaries for Octave on the Octave site. Windows binaries are a little more difficult to find in an easytouse form. There is a binary that you can install by extracting a compressed file to the correct directories, or you can use the Windows installer version available in our codebin. The Windows installer version isn't quite uptodate but it is perfectly OK for learning Octave and it is the version used on the Coursera Machine Learning course.
Interactive and Persistent
Programming in Octave is slightly different from most languages  as it is a persistent programming environment. What this means is that you work at a command prompt and any variables you create persist for the session. Anything you type is evaluated as soon as you press the return key and the result is displayed  unless you finish the line with a semicolon when the output is suppressed.
That is if you type:
A=10
then Octave prints back:
A=10
If you later type:
A
it once again prints:
A=10
Once you have created a variable at the command line, it stores whatever value you have given it until you close Octave down or use the clear command.
If you type clear then all variables are removed from the environment and if you then type A you will see the error message "error 'A' undefined near line x column y"
This retained or persistent mode is great for interactive programming. For example, you can read or type in a data matrix and then try out commands to compute whatever you are trying to get from it.
This is a good way to learn Octave but at some point you will want to write something that looks more like a traditional program that you load and run. This is very easy but it is important to realize that this all works within the persistent environment and code that you read in from a file is treated just like code you type in. That is after the code has run the environment has changed according to what variables the code created and modified. This means you can work by running one program on some data you typed in and then run another program to process the results of the first program.
If this very interactive style of working worries you  don't fret because you can create programs that work in isolation simply by starting them off with the clear command.
Programs
So how do you create an Octave program?
The answer is you can simply put the code that you would have typed at the command line into a file with a name that ends in .m. You can use any editor you care to but NotePad or NotePad++ are good choices. You can invoke the editor within Octave by typing edit filename which editor you get depends on the system and how it has been configured.
To run an Octave program you simply type its name at the command prompt. Of course the file has to be stored in the current directory and you need to know that the ls command will list the contents of the current directory and the cd will change the directory in the usual way.
If you have created a program in a file and when you type the name Octave cannot find it and reports an "undefined" error then it is most likely that the file isn't in the current directory.
So for example if you prepare a file with the commands:
A=10; A
and save it as Test.m in the current directory when you type:
Test
at the command prompt you will see displayed:
A=10
Notice that after the program Test has run, A is still defined and has the value 10 so you can carry on using it by typing in commands at the command prompt or by running other programs that use a variable called A.
Also notice that Octave is case sensitive  if you saved the file as Test.m you have to type Test  test, TEST, tesT or any other form just doesn't work.
If the file contains a function  see later  then you can call the function just by typing the file name followed by brackets and any arguments. For example if Test.m contains a function you can write Test() to run the function.
Matrices
The central data structure in Octave is the matrix and the sooner we get to grips with it the better.
You define a variable by using it, i.e. by assigning it a value. Variables are not typed and you can store anything in a variable at any time as long as it makes sense.
Numbers can be integer, floating point and complex. Complex values use i or j for the imaginary component.
For example:
A=1 integer A=0.1 floating point A= 1E3 floating point A= 1+2i complex
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..
All values in Octave are represented internally as double precision value (including integers). There are builtin functions for working with real and complex values.
There is also a special missing data value, NA, which can be used to implement statistical procedures that recognize missing data.
Logical values are represented by 1 as true and 0 as false. There are also strings that can be stored in variables and manipulated, and these work in the way you would expect.
What makes Octave special is the ease with which you can create matrices and work with them.
A matrix is define using square brackets to contain a list of numbers. Matrices are one or twodimensional. Working with multidimensional structures is possible but more complicated.
When typing in a matrix the comma separator means move on one column and the semicolon means move on one row.
So for example:
A=[1,2,3;4,5,6]
defines a 2x3 matrix
1 2 3
4 5 6
Matrices cannot be irregular  if the first row has three values then the subsequent rows must have three values.
Obviously
A=[1,2,3]
is a row vector and
A=[1;2;3]
is a column vector.
