Analytics Using MATLAB and Octave-Free Learning Series– Introduction & Basic Functions

MATLAB stands for MATrix LABoratory. It was designed to make matrix manipulation easy for scientific computations.According to Mathworks website,MATLAB is the high level interactive programming environment used by Millions of scientists, engineers, analytics professionals worldwide. Learning Octave & MATLAB are the best available tools for prototyping the algorithms of machine learning.
This is what widely followed in Silicon valley as well. A lot of companies which work on artificial intelligence or machine learning algorithms first try to prototype the concept and when they make sure that the algorithm can behave as they wanted it to be, then they work on full blown scalable applications which can then be programmed in Java or C++ to achieve the efficiency.

The advantage of using MATLAB/Octave

There are several advantages of using MATLAB & Octave for machine learning and general analytics. Some of these are mentioned below:

  • These are the environments which provide scripting and programming support both. Just like Python or Bash, you can just try small snippets of code on the prompt and see the output.
  • A large support for image processing and speech recognition. Support provided for integration with libraries such as OpenCV.
  • MATLAB can generate automatically the equivalent C code which can be used to make better and efficient systems.
  • Community support is the best feature of MATLAB environment. It is a popular programming environment and you can get almost any kind of solution to any problem on Internet related to MATLAB.

The graphical support provided by MATLAB is extremely powerful.It is easy to generate graphs such as histograms and box plots easily using MATLAB and Octave.

  • MATLAB and Octave are the languages for technical computing.This helps in mathematical computations, modelling, simulations, data analysis and processing,visualization & graphics and algorithms development.
  • The MATLAB environment allows the user to manage thousands of variables, help moving the data, develop and manage script files with MATLAB.

The Disadvantage of using MATLAB

MATLAB is highly expensive. Though people use unlicensed copies of software but that is not the preferred way to use the tool. MATLAB is also slow compared to other programming languages. In order to make it easily accessible to anyone in world, there is an open source distribution Octave. Octave is similar to MATLAB and accepts almost all sorts of code which we want to run on MATLAB. It is cheaper to use Octave for prototyping and academic purposes. There are big companies which use Octave heavily for their internal coding purposes.

Analytics Using MATLAB and Octave

The data science field is not restricted to Python or R. A data scientist has dozens of tools at his disposable which he can use to write big applications and also for prototyping. After taking an online class of Machine learning conducted by Stanford University’s professor Andrew NG, I got to know that Octave is the preferred programming environments for prototyping Machine learning algorithms. For example, if you have two different sounds mixed with one another, you can separate the two sounds from each other using just one line of code as following:

[w,s,v]= svd((repmat(sum(x.*x,1),size(x,1),1).*x)*x');

Readers who are interested to know how this line of code works, they should go deeper for speech recognition API’s of MATLAB and Octave. I have started a series of programming tutorials on MATLAB and Octave so that users who are getting started with machine learning and analytics, can also add a tool to their skillset. Apart from introduction part, you can easily follow along the tutorials and I am sure within no time you will be running the MATLAB and octave on your system.

What are the Prerequisites of Getting Started

There are no prerequisites of using this tutorial series. You can just follow along, take the code, run it in your interpreter and see the output. This red-experiment-understand approach will make you an expert with in a short span of time and you will be ready to crunch the data very soon. It is good if you experience in any programming language such as C,C++, Python or Java.
Many of the concepts will be used here as well. So your knowledge is not going to be wasted. Some level of understanding of linear algebra is required as you will be working with Matrices, Vectors, Inverse matrix and their transposes as well. It is also good to have some statistics knowledge under your belt,as you will be exposed to various functions related to hypothesis testing, regression and descriptive statistics.

Where to Download MATLAB and Octave

If you are a business and want to build heavy applications, then you should definitely consider purchasing the MATLAB license otherwise you can download the open source octave distribution using this download link.

What Will Be Covered in This Series

In this MATLAB and Octave series we will cover below topics:

  • Introduction to MATLAB and getting started lessons
  • Manipulation of Matrix: The building blocks of MATLAB and learn more about 1D and 2D arrays
  • How Loops work in MATLAB and octave
  • We will learn about conditional statements and nested looping
  • How to create user defined functions?
  • How to generate graphs and plots

How the Screen Will look like

For those who have installed MATLAB, here is a description of MATLAB workspace which includes:

  • Editor Window: This is where you write your code in the scripting file.
  • Workspace window: This displays all the variables which you create in your namespace.
  • Command Window: This is where you can directly execute the commands without writing them in a file.
  • Current Directory Window: This will show all files and folders in the current directory.

How to Get Help In MATLAB

You can use help or doc command with any function. For example, if I want to find help on pinv function which ius used to find the inverse matrix of any matrix, if exists.

>>> help pinv

Basics to Help You Get Started

  • You always need to type a command next to the prompt.
  • To insert comments before the line or inline always use percentile sign. For example- % this is a comment.
  • As soon as you execute any command, MATLAB will run it instantly and display the output. If you don’t want to display the output, use a semicolon(;) at the end of line.
  • Use clc command to clear the command window.
  • To display the variables in memory use who command. You can also use whos command which will display all the variables along with other details such as their size also.
  • Use clear command to clear the variables from namespace. For example, if you want to delete the variable x from memory, use clear x or if you want to clear the whole namespace, just type clear.

What is a Script File

Script file is a file where you store your MATLAB or Octave commands so that it becomes a whole program. All commands are executed in the linear order. In case, you see a red mark in line, that indicates that there is an error in your code. These script files are called M- files and have extension .m.

Basic Arithmetic Operations

Addition=A + B
Subtraction=A - B
Multiplication=A * B
Right division=A / B
Left division=A \ B
Exponentiation=A ^ B
Element wise multiplication=A .* B
Element wise right division=A ./ B
Element wise left division= A .\ B
Element wise exponential= A .^ B
Transpose of matrix= A’

What is the Order of Precedence

This tutorial already assumes that you have basic knowledge of order of precedence. Here is the order of precedence in context of operators which is followed in MATLAB and Octave programming languages.

()
^
* and /, \
+ and -

Relational Operators

Less than = A < B
Greater than = A > B
Less than and equal to = A <= B
Greater than and equal to = A >= B
Equal to = A == B
Not equal to = A ~= B
Assignment operator = A = B
Assignment operator (A = B) indicates that the value of B
is assigned to A

What are Identifiers

  • All the words that build the program such as himax,matlab_tutorial etc.
  • Identifiers should not start with digits.
  • Should not be a reserved word such as pi, NaN,eps, inf etc
  • Maximum length should not be more than 63 characters.

Display Formats

You can customize the output of the commands by defining the format at the beginning of the program. Few important commands are:

format short => fixed point, 4 decimal digits
format long => fixed point, 15 decimal digits
format short e => Scientific notations with 4 decimal digits
format long e => Scientific notations with 15 decimal digits
format bank => Two decimal digits
format hex => hexadecimal
Format rat => approximate integer ratio

Elementary Math Functions of MATLAB and Octave

a=sqrt(x); % find square root of x and assign the result to a
a=nthroot(x,n); % find nth root of x
a=exp(x); % find exponential of x
a=log(x); %find natural logarithm of x
a=log10(x); % base 10 logarithm
a=log2(x);  %base 2 logarithm
a=factor(x) %provides prime factors of x
a=factorial(x) %provides the factorial of x
a=perms([x1 x2 x3]) %all possible permutations of x1,x2 and x3

Rounding and Remainder Functions of MATLAB/Octave

a=ceil (x); %/round towards +ve infinity
a=fix (x); %round towards zero
a=floor (x); %round towards -ve infinity
a=rem (x,y); % remainder after dividing x by y
a=round (x); %round to nearest integer
a=sign (x); % return sign of the number (for zero, it returns 0)

What are the Predefined Variables

ans
pi = 3.14
eps = 2^(-52), i.e., smallest difference between two numbers.
inf = Infinity
NaN = Not a number

This was the first chapter of the series. I hope you liked it.

Aly Chiman

Aly Chiman is a Blogger & Reporter at AlyChiTech.com which covers a wide variety of topics from local news from digital world fashion and beauty . AlyChiTech covers the top notch content from the around the world covering a wide variety of topics. Aly is currently studying BS Mass Communication at University.