Octave Basics

Octave is a great language for prototyping machine learning algorithms. It is recommended to use Octave to prove out your algorithms and then implementing your machine learning algorithm in C++ or Java. Other common languages for prototyping machine learning algorithms are Matlab, R, Python, and NumPy.

Basic Commands

> 5+6
> 1/2
> 2^6
> 1==2
> 1 ~= 2
> help eye    

Comment

> 5 + 6 % this is a comment

Change Octave Prompt

PS('>> ');

Variables

> a = 3
> a = 3; % semicolon suppresses output
> b = 'hi';
> c = (5 > 2);
> a= pi;
> disp(a);
> disp(sprintf('%0.2f', a));
> format long
> format short

Matrices

> A = [1 2; 3 4; 5 6]
> V = [1 2 3]
> B = 1:0.1:2
> ones(2,3)
> C = 2 * ones(2,3)
> w = rand(1,3)
> z = rand(3,3)
> w = randn(1,3)
> I = eye(3)

Histogram

> hist(w)
> hist(w, 50)

Size and Length of Vectors

> A = [1 2; 3 4; 5 6]
> size(A)
> size(A, 1)
> size(A, 2)

Workspace

> who % variables
> whos % variables in current scope
> clear A % remove matrix A

Data

> load('weather.dat')
> save hello.mat v;
> save hello.txt -ascii; % save as text

Matrix Manipulation

> A(2,:) % row 2
> A(:,2) % column 2
> A([1 3], :) % row 1 and row 3
> A(:) % put all elements into a single vector

Append another column

> A = [A, [100; 200; 300]]

Combine two matrices

> C = [A B]
> C = [A; B]

Mac OS X Installation Tip

I had to install Quartz to get Octave up and running on Mac OS X 10.8.5.

When plotting using gnuPlot I ran into this error message.

dyld: Library not loaded: /opt/X11/lib/libfreetype.6.dylib
Referenced from: /usr/X11/lib/libfontconfig.1.dylib
Reason: Incompatible library version: libfontconfig.1.dylib requires version 15.0.0 or later, but libfreetype.6.dylib provides version 13.0.0

You can fix this issue by following theses instructions

Open /Applications/Gnuplot.app/Contents/Resources/bin/gnuplot in a text
editor. Use the editor search-and-replace feature to replace
"DYLD_LIBRARY_PATH" with "DYLD_FALLBACK_LIBRARY_PATH". There are four
instances that need to be replaced.

References