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.
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.
Instead of replicating a tutorial, below are commands you will find helpful when running your machine learning experiments.
|Elementary math operations|
|Logical equal to|
|Logical not equal to|
|n x n identity matrix|
|n x m matrix of ones|
|n x m matrix of randoms|
|n x m matrix of random integers|
|Dimensions of matrix|
|Clear command window|
|Transpose of matrix|
|Get the 2nd row of|
|Get the last row using the end keyword|
|Pseudo inverse of matrix|
|Variable assignment - semicolon suppresses output|
|A 3x2 matrix with denoted elements|
|A 3x1 column vector i.e|
|Creates a matrix|
|Creates a matrix values from 0 to 4 with spacing of 2|
|Useful when creating a range of values (no need to calculate spacing yourself)|
|Shows man page for eye|
|Open doc page for eye|
|Print working directory|
|Save variables in workspace to a MAT-file|
|Load saved workspace variables|
|Increase precision (default is short)|
|Show variables in the current scope|
|Show 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.
|Maximum element in|
|Round each element to the nearest integer|
|Elements that are less than 3|
|Generate a n x n magic matrix|
|Assuming is an n x n matrix then this yields the diagonal and all other elements are zero|
|Flip matrix up-down|
|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.|
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
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
Visualise a matrix.
% Plot an 8x8 matrix with a color map (using command chaining) imagesc(magic(8)), colorbar % Close the plot close
% 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 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.
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.
It is easy to see how vectorisation works.