# An Octave primer

Here is a simple Octave Primer. Octave is a powerful language for implementing Machine Learning algorithms. As I have mentioned its strength is its simplicity. I am including some basic commands with which you can get by implementing fairly complex code

%%Matrix
A matrix can be created as `a = [1 2 3; 4 7 8; 12 35 14]; `% This is 3 x 3 matrix
Matrix multiplication can be done between m x n * n x k matrix as follows

```a = [4 56 3; 2 3 4]; b = [23 1; 3 12; 34 12]; % a = 3 x 2 matrix b = 2 x 3 matrix c = a*b; %% c = 3 x 2 * 2 * 3 = 3 x 3 matrix```

```c = 362 712 191 86```

%%Inverse of a matrix can be obtained by
```d = pinv(c); octave-3.2.4.exe:37> d = pinv(c) d = -8.2014e-004 6.7900e-003 1.8215e-003 -3.4522e-003```

%%Transpose of a matrix
`e = c'; % e is the transpose of done`

```octave-3.2.4.exe:38> e = c' e = 362 191 712 86```

The following operations are done on all elements of a matrix or a vector
```k = 5; a = [1 2; 3 4; 5 6]; k = 5.23; c = k * a; d = a - 2 e = a / 5 f = a .* a % Dot product g = a .^2; % Square each elements```

%% Select slice of matrix
```b = a(:,2); % Select column 2 of matrix a (all rows) c = a(2,:) % Select row of matrix 'a' (all columns)```

```d = [7 8; 8 9; 10 11; 12 13]; % 4 rows 2 columns d(2:3,:); %Select from rows 2 to 3 (all columns)```

```octave-3.2.4.exe:41> d d = 7 8 8 9 10 11 12 13 octave-3.2.4.exe:43> d(2:3,:) ans = 8 9 10 11```

%% Appending rows to matrix
```a = [ 4 5; 5 6; 5 7; 9 8]; % 4 x 2 b = [ 1 3; 2 4]; % 2 x 2 c = [ a; b] % stack a over b d = [b ; a] % stack b over a*b ```
```octave-3.2.4.exe:44> a = [ 4 5; 5 6; 5 7; 9 8] % 4 x 2 a = 4 5 5 6 5 7 9 8```

octave-3.2.4.exe:45>``` b = [ 1 3; 2 4] % 2 x 2 b = 1 3 2 4```

octave-3.2.4.exe:46> ```c = [ a; b] % stack a over b c = 4 5 5 6 5 7 9 8 1 3 2 4```

octave-3.2.4.exe:47> ```d = [b ; a] % stack b over a*b d = 1 3 2 4 4 5 5 6 5 7 9 8```

%% Appending columns
```a = [ 1 2 3; 3 4 5]; b = [ 1 2; 3 4]; c = [a b]; d = [b a];```

```octave-3.2.4.exe:48> a = [ 1 2 3; 3 4 5] a = 1 2 3 3 4 5```

octave-3.2.4.exe:49>``` b = [ 1 2; 3 4] b = 1 2 3 4```

octave-3.2.4.exe:50> ```c = [a b] c = 1 2 3 1 2 3 4 5 3 4```

octave-3.2.4.exe:51>``` d = [b a] d = 1 2 1 2 3 3 4 3 4 5 %%Size of a matrix [c d ] = size(a); ```
Creating a matrix of all zeros or ones
```d = ones(3,2); e = zeros(4,3); ```
%Appending an intercept term to a matrix
```a = [1 2 3; 4 5 6]; %2 x 3 b = ones(2,1); a = [b a];```

%% Plotting
Creating 2 vectors
```x = [1 3 4 5 6]; y = [5 6 7 8 9]; plot(x,y); ```
%%Create labels
```xlabel("X values); ylabel("Y values); axis([1 10 4 10]); % Set the range of x and y title("Test plot);```

%%Creating a 3D scatter plot
If we have 3 column csv file then we can load the data as follows
```data = load('values.csv'); X = data(:, 1:2); y = data(:, 3); scatter3(X(:,1),X(:,2),y,[],[240 15 15],'x'); % X(:,1) - x axis X(:,2) - yaxis y[] - z axis```

%% Drawing a 3D mesh
```x = linspace(0,xrange + 20,10); y = linspace(1,yrange+ 20,10); [XX, YY ] = meshgrid(x,y); ```
[a b] = size(XX)

Draw the mesh
```for i=1:a, for j= 1:b, ZZ(i,j) = [1 (XX(i,j)-mu(1))/sigma(1) (YY(i,j) - mu(2))/sigma(2) ] * theta; end; end; mesh(XX,YY,ZZ);```

%% Creating different polynomial equations
Let X be a feature vector
then
X = [X X.^2 X^3] %X X^2 X^3

This can be created using a for loop as follows
```for i= 1:n xtemp = xinput .^i; x = [x xtemp]; end;```

Finally while doing multivariate regression if we wanted to create polynomial terms of higher we could do as follows. Let us say we have a feature vector X made of 3 features x1, x2,

Let us say we wanted to create a polynomial of the form x1^2 x1.x2 x2^2 then we could create X as

`X = [X(:,1) .^2 X(:,1) . X(:,2) X(:,2) .^2]`

As you can see Octave is really powerful language for Machine Learning and has just a few handful of constructs with which one can implement powerful Machine Learning algorithms

## 2 thoughts on “An Octave primer”

1. Rohit says:

Good primer
Can become better with a little proof reading

c = a*b; %% c = 3 x 2 * 2 * 3 = 3 x 3 matrix
d = [b ; a] % stack b over a*b
and few more

Like

1. Tinniam V Ganesh says:

Thanks Rohit. Corrected.

Like