Getting started with GNU Octave (or MATLAB) for machine learning

6 min read

    82% of Fortune 100 companies use MATLAB; Octave allows you to prototype your Machine Learning experiments faster, saving you time and money. Since it's a high level programming language you can do things really fast. Some people prefer to get their algorithms working in MATLAB and then migrate to another language such as Python, R, Java or C++. So learning to use MATLAB is a good investment.

    MathWorks provides a free online self-paced MATLAB Course. You also get a certificate which you can share to show others you have completed the course. The course is really well made.

    Installation

    Since MATLAB has a price tag, we will use an open source alternative called Octave. Octave is largely compatible with MATLAB - so you can still follow along if your're a MATLAB user.

    On Ubuntu 20.04

    sudo apt update
    sudo apt install octave

    For other distributions, please consult GNU Octave download website.

    Basics

    Instead of replicating a tutorial, below are commands you will find helpful when running your machine learning experiments.

    Command(s)Usage
    +, -, \\, *Elementary math operations
    ==Logical equal to
    ~=Logical not equal to
    &Logical AND
    |Logical OR
    xor(a, b)Logical XOR
    eye(n)n x n identity matrix
    ones(n, m)n x m matrix of ones
    rand(n, m)n x m matrix of randoms
    randi(n, m)n x m matrix of random integers
    size(A)Dimensions of matrix
    clear ARemove variable
    clcClear command window
    A'Transpose of matrix
    A(1,2)Get element
    A(2,:)Get the 2nd row of
    A(end, :)Get the last row using the end keyword
    pinv(A)Pseudo inverse of matrix
    a = 3Variable assignment
    a = 3;Variable assignment - semicolon suppresses output
    piConstant PI
    A = [1 2; 3 4; 5 6]A 3x2 matrix with denoted elements
    V = [1; 2; 3]A 3x1 column vector i.e
    1:4Creates a matrix
    0:2:4Creates a matrix values from 0 to 4 with spacing of 2
    linspace(first, last, num_elements)Useful when creating a range of values (no need to calculate spacing yourself)
    help eyeShows man page for eye
    doc eyeOpen doc page for eye
    pwdPrint working directory
    cdChange directory
    lsList files
    load someData.datLoad data
    save filename variableSave variables
    save workspace.matSave variables in workspace to a MAT-file
    load workspace.matLoad saved workspace variables
    format longIncrease precision (default is short)
    whoShow variables in the current scope
    whosShow variables in the current scope with more info

    Watch out: in Octave/MATLAB vectors are not zero-indexed i.e. they start at one

    Note: All MATLAB variables are arrays. Understand the difference between matrix, column vector, row vectror and scalar. Fun fact: MATLAB is an abbreviation for MATrix LABoratory - makes sense.

    Tip: If you only use one index with a matrix, it will traverse down each column in order. Using one index, try extracting the eighth element of data.

    Manipulating data (matrices/vectors)

    CommandUsage
    A * BMatrix multiplication
    A .* BElement-wise multiplication
    abs(V)Element-wise absolute
    exp(V)Element-wise exponentiation
    log(V)Element-wise log
    max(A)Maximum element in
    round(A)Round each element to the nearest integer
    a < 2Element-wise comparison
    find(A < 3)Elements that are less than 3
    sum(A)Sum of
    magic(n)Generate a n x n magic matrix
    A .* eye(n)Assuming is an n x n matrix then this yields the diagonal and all other elements are zero
    flipud(A)Flip matrix up-down
    A(A > 2)Logical indexing. You can use a logical array as an array index, in which case MATLAB extracts the array elements where the index is true.

    Plotting data

    Plot the function.

    x = [0:0.01:0.98];
    y = sin(2 * pi * 4 * x);
    
    plot(x, y);

    Plot the and function on the same graph.

    % Create an array from 0 to 0.98 in steps of 0.01
    x = [0:0.01:0.98];
    
    % Sine function
    y_sin = sin(2 * pi * 4 * x); % notice the use of x
    
    % Cosine function
    y_cos = cos(2 * pi * 4 * x);
    
    % Multple plots using 'hold on'
    plot(x, y_sin);
    hold on;
    plot(x, y_cos, 'r'); % cos function in red
    
    % Tip: use xlim([xmin xmax]) to zoom in on an area of interest
    
    % Add title, axis labels and legend
    title('Plot title')
    xlabel('x-value')
    ylabel('y-value')
    legend('sin', 'cos')
    
    % Save/export the plot as png
    print -dpng 'plot.png'
    
    % See plot gallery at https://uk.mathworks.com/products/matlab/plot-gallery.html

    Use figure to create multiple figures. Tip: use help plot for more information on how to use the plot command. Use subplot to plot side by side.

    Visualise a matrix.

    % Plot an 8x8 matrix with a color map (using command chaining)
    imagesc(magic(8)), colorbar
    
    % Close the plot
    close

    Control statements

    % for loop
    for i = 1:10
        % double each element of matrix A
        A(i) = 2 * A(i);
    end
    
    % while loop
    i = 0;
    while i <= 10
        i = i + 1;
    end
    
    % if, elseif, else
    if i == 5
        disp('Found a 5');
    elseif i == 4
        disp('Found a 4');
    else
        disp('Keep searching');
    end

    Functions

    Functions are defined in files such as addTwoNumbers.m. Make sure your function is in your octave search path or current working directory.

    % File: addTwoNumbers.m
    
    function y = addTwoNumbers(a, b)
    y = a + b;

    Pro tip: Octave can natively return multiple values.

    Vectorisation (advance topic)

    By using the vectorised implementation we can take advantage of matrix multiplication provided by the linear algebra library which will be faster than implementing a summation yourself (usually with a for loop). Below is an example.

    where

    It is easy to see how vectorisation works.

    Further reading