Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Last active March 18, 2018 18:29
Show Gist options
  • Save tjunghans/d06255c24618d56c4efce9ea463d6afc to your computer and use it in GitHub Desktop.
Save tjunghans/d06255c24618d56c4efce9ea463d6afc to your computer and use it in GitHub Desktop.
Octave Commands - Standford Machine Learning

Octave

This is a summary of the Octave Tutorial of the Coursera Machine Learning course by Andrew Ng.

Basic Operations

>> 5 + 6
ans = 11

>> 1 == 2
ans = false

>> 1 ~= 2 % not equal
ans = 1 

>> 1 && 0  % AND
ans = 0

>> 1 || 0  % OR
ans = 1

Variables

>> a = 3
a =   3
>> a = 3;  % semicolon supressing output

>> b = 'hi^';
>> b
b = hi

>> c = (3>=1);
>> c
c =  1

>> a=pi;
>> a
a =  3.1416
>> disp(a);
 3.1416
>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14
>> disp(sprintf('6 decimals: %0.6f', a))
6 decimals: 3.141593

>> format long
>> a
a =  3.14159265358979
>> format short
>> a
a = 3.1416

Vectors and Matrices

>> A = [1 2; 3 4; 5 6]
A = 

  1    2
  3    4
  5    6
  
>> A = [1 2;
> 3 4;
> 5 6]
A = 

  1    2
  3    4
  5    6
  
>> V = [1 2 3]
V = 

  1    2    3
  
>> V = [1; 2; 3]
V = 

  1
  2
  3
  
>> V = 1:0.1:2 % increments 1 to 2 in steps of 0.1
V = 

Columns 1 through 11:

   1.0000    1.1000    1.2000 ... 2.0000
   
>> V = 1:6
V = 
  
  1    2    3    4    5    6

>> ones(2, 3)
ans = 

   1   1   1
   1   1   1
 
>> C = 2*ones(2, 3)
C = 

   2   2   2
   2   2   2
   
>> C = [2 2 2; 2 2 2]
C = 

   2   2   2
   2   2   2
   
 >> w = zeros(1, 3)
 w = 
 
   0   0   0
   
 >> w = rand(1,3)  % 1 by 3 matrix with random numbers
 >> w = randn(1, 3) % gaussian random numbers
 
 >> eye(4)  % 4 by 4 identity matrix

Getting help

 >> help eye %  brings up the documentation for the eye command

Moving Data Around

>> A = [1 2; 3 4; 5 6]
A = 

  1    2
  3    4
  5    6
  
>> size(A)
ans = 

  3   2
  
>> sz = size(A)
sz = 
  
  3    2
  
>> size(sz)
ans = 

  1   2
  
>> size(A, 1)  % num rows
ans =  3

>> size(a, 2)  % num columns
ans =  2

>> v = [1 2 3 4]
>> length(v)
ans =  4

>> length(A)  % prints out longer dimension which is 3 and it's also confusing, so rather just use for vectors

>> pwd  % shows path of octave

>> load faturesX.dat  % load file
>> load('featuresX.dat')  % same as above

>> who  % shows variables in the current scope
>> whos  % detailed view

>> clear featuresX  % removes variable

>> myVector(1:10)  % gets elements 1 to 10

>> save hello.mat myVector  % saves data compressed to file
>> save hello.txt myVector  % saves data in human readable format

Manipulate Data

>> A = [1 2; 3 4; 5 6]

>> A(3, 2) % 6

>> A(2, :)  % gets every element in row 2

>> A(:, 2)  % gets every element in column 2

>> A([1 3], :)  % gets every element from rows 1 and 3

>> A(:, 2) = [10; 11; 12]  % assign new values to second column

>> A = [A, [100; 101; 102]];  % add another column to A

>> A(:) % put all elements of A into a single vector

>> A = [1 2; 3 4; 5 6]
>> B = [11 12; 13 14; 15 16]
>> C = [A B]  % same as [A, B]
C = 

  1    2    11   12
  3    4    13   14
  5    6    15   16
  
>> C = [A; B]  % next line
C = 

  1    2
  3    4
  5    6
  11   12
  13   14
  15   16
  

Computing on Data

% Setup variables
>> A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

>> B = [11 12; 13 14; 15 16]
B =

   11   12
   13   14
   15   16

>> C = [1 1; 2 2]
C =

   1   1
   2   2

>> A*C  % multiplication
ans =

    5    5
   11   11
   17   17
   
>> A.*B  % element wise multiplication
ans =

   11   24
   39   56
   75   96 

>> V = [1; 2; 3]
V =

   1
   2
   3

>> 1./V  % element wise inverse
ans =

   1.00000
   0.50000
   0.33333

>> 1 ./ A  % matrix inverse
ans =

   1.00000   0.50000
   0.33333   0.25000
   0.20000   0.16667    

>> -V  % -1*V
ans =

  -1
  -2
  -3

>> abs([-1; 2; -3])
ans =

   1
   2
   3

>> length(V)
ans =  3

>> V + ones(length(V), 1)  % adding 1 to each item
ans =

   2
   3
   4

>> A'  % A transpose
ans =

   1   3   5
   2   4   6

>> a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

>> max(a)
ans =  15

>> [var, ind] = max(a)
var =  15
ind =  2

>> a < 3  % returns 1 if element in a is smaller than 3 or 0
ans =

  1  0  1  1

>> find(a < 3)
ans =

   1   3   4


>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> sum(a)
ans =  18.500
>> prod(a)
ans =  15
>> floor(a)
ans =

    1   15    2    0
>> ceil(a)
ans =

    1   15    2    1


>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> max(A, [], 1)  % find maximum of each column
ans =

   8   9   7
   

Proving the magic function

>> A = magic(9)  % all rows, columns and diagonals sum up to the same value
A =

   47   58   69   80    1   12   23   34   45
   57   68   79    9   11   22   33   44   46
   67   78    8   10   21   32   43   54   56
   77    7   18   20   31   42   53   55   66
    6   17   19   30   41   52   63   65   76
   16   27   29   40   51   62   64   75    5
   26   28   39   50   61   72   74    4   15
   36   38   49   60   71   73    3   14   25
   37   48   59   70   81    2   13   24   35

>> sum(A, 1)  % sum up columns
ans =

   369   369   369   369   369   369   369   369   369

>> sum(A, 2)  % sum up rows
ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369


>> eye(9)
ans =

Diagonal Matrix

   1   0   0   0   0   0   0   0   0
   0   1   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0
   0   0   0   1   0   0   0   0   0
   0   0   0   0   1   0   0   0   0
   0   0   0   0   0   1   0   0   0
   0   0   0   0   0   0   1   0   0
   0   0   0   0   0   0   0   1   0
   0   0   0   0   0   0   0   0   1

>> A.*eye(9)
ans =

   47    0    0    0    0    0    0    0    0
    0   68    0    0    0    0    0    0    0
    0    0    8    0    0    0    0    0    0
    0    0    0   20    0    0    0    0    0
    0    0    0    0   41    0    0    0    0
    0    0    0    0    0   62    0    0    0
    0    0    0    0    0    0   74    0    0
    0    0    0    0    0    0    0   14    0
    0    0    0    0    0    0    0    0   35

>> sum(sum(A.*eye(9)))  % sum up diagonal values
ans =  369

>> A.*flipud(eye(9))
ans =

    0    0    0    0    0    0    0    0   45
    0    0    0    0    0    0    0   44    0
    0    0    0    0    0    0   43    0    0
    0    0    0    0    0   42    0    0    0
    0    0    0    0   41    0    0    0    0
    0    0    0   40    0    0    0    0    0
    0    0   39    0    0    0    0    0    0
    0   38    0    0    0    0    0    0    0
   37    0    0    0    0    0    0    0    0

>> sum(sum(A.*flipud(eye(9))))  % flips the identity matrix
ans =  369

Getting the Inverse of a Matrix

>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> pinv(A)  % sudo inverse 
ans =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>> A * pinv(A)  % this gives us the identity matrix
ans =

   1.0000e+00  -1.2212e-14   6.5503e-15
   5.5511e-17   1.0000e+00  -1.1102e-16
  -5.8842e-15   1.2434e-14   1.0000e+00

>> abs(round(A * pinv(A)))  % the above cleaned up
ans =

   1   0   0
   0   1   0
   0   0   1

Plotting Data

Sinus curve

>> t=[0:0.01:0.98];
>> y1=sin(2*pi*4*t);
>> plot(t, y1);  % opens window with graph

Cosinus curve in same window

>> y2= cos(2*pi*4*t);
>> plot(t, y1)
>> hold on;  % the next plot will not replace but will be added to existing window
>> plot(t, y2, 'r');  % plot in red color
>> xlabel('time')  % label x axis
>> ylabel('value')  % label y axis
>> legend('sin', 'cos')
>> title('my plot')
>> print -dpng 'myPlot.png'

>> close  % close figure window

Opening another window/figure

>> figure(1); plot(t, y1);  % label figure window
>> figure(2); plot(t, y2);  % different label results in new window

Plotting with subplots

>> subplot(1,2,1);
>> plot(t, y1)
>> subplot(1, 2, 2);
>> plot(t, y2);
>> axis([0.5 1 -1 1]);

Display a matrix in color

>> subplot(1,2,1);
>> plot(t, y1)
>> subplot(1, 2, 2);
>> plot(t, y2);
>> axis([0.5 1 -1 1]);

Control Statements: for, while, if statement

For Loops

>> v=zeros(10, 1)
v =

   0
   0
   0
   0
   0
   0
   0
   0
   0
   0

>> for i=1:10,
>   v(i) = 2^i;
> end;
>> v
v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024


% another example

>> indices=1:10;
>> for i=indices,
>    disp(i);
> end;
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
>> 

### While Loop

>> i = 1;
>> while i <= 5,
>   v(i) = 100;
>   i = i + 1;
> end;
>> v
v =

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024


% another example with break

>> i = 1;
>> while i <= 5,
>   v(i) = 100;
>   i = i + 1;
> end;
>> v
v =

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024


### If Statement

>> v(1)
ans =  999
>> v(1) = 2;
>> if v(1)==1,
>   disp('The value is one');
> elseif v(1) == 2,
>   disp('The value is two');
> else
>   disp('The value is not one or two.');
> end;
The value is two

Functions

Functions are defined in files. The file name corresponds to the function name and the suffix is m. Octave will find the function files in the current directory. Otherwise you need to change directories or set the search path and add the path using addPath.

% y is the return value and needs to be assigned to the result
function y = squareThisNumber (x)

  y = x^2;


% multiple return values
function [y1, y2] = squareAndCubeThisNumber(x)

  y1 = x^2;
  y2 = x^3;


% usage
>> [a, b] = squareAndCubeThisNumber(5)
a =  25
b =  125

Define a function to compute the cost function J of theta

% design matrix, x values of training examples
>> x = [1 1; 1 2; 1 3]
x =

   1   1
   1   2
   1   3

>> y = [1; 2; 3]
y =

   1
   2
   3

>> theta = [0;1];
>> j = costFunctionJ(x, y, theta)
j = 0
>> theta = [0;0];
>> j = costFunctionJ(x, y, theta)
j =  2.3333
>> 

% test to see if costFunctionJ computes correct

>> (1^2 + 2^2 + 3^2) / (2*m)
ans =  2.3333

costFunctionJ

function J = costFunctionJ(X, y, theta)

% X is the "design matrix" containing our training examples
% y is the class labels

m = size(X, 1); % number of training examples
predictions = X*theta; % predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2; % squared errors

J = 1/(2*m) * sum(sqrErrors);

Vectorization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment