Matrix and Vectors Construction

Return to Matlab Tutorials

In this tutorial you will learn:

  1.  Matrix and vector Construciton.
  2. Zero Matrix
  3. Ones Matrix
  4. Identity Matrix
  5. Linerally spaced vectors
  6. Logarithmically spaced vectors

So lets get started!

Matrices

Matrices in Matlab are one of the prime reasons for user to stick around.  It is called MATrixLABoratory for a reason. Otherwise, there are better alternatives when it comes to other features.

Now, a matrix has rows and columns. Kinda looks like this 😀

So, how do we make this sucker in Matlab?

Like this!

Or, you can go like this to save space:

m = [ 1, 1, 0;0, 1, 0;0, 0, 1];

A few more matrices are:

Column Vector:  m = [1;2;3;4]

Row vector: m = [1,2,3,4]

A 2×3 vector m = [1,2 ; 3,4 ; 5,6]

Special Matrices

I am calling them special matrices because these matrices have a special property which can be exploited for our purpose:

Zero Matrix

These matrices have nothing but zeroes in them. They are declared like:

zeros(20, 7)

Ones Matrix

Contrary to Zero Matrix, Ones matrix have every element set as “1”. These matrices are declared like,

ones(4, 20)

Identity Matrix

An identity matrix has ones in the diagonal while zeroes on every other element. It is made like this:

eye(10)

eye(4, 6)

 

Linearly Spaced Vector

This command creates an evenly spaced vector between two numbers. The format is,

linspace(startingNumber, endingNumber, numberOfPoints)

e.g.

linspace(1, 10, 10)

Logarithmically spaced vectors

This command creates Logarithmically spaced vector between two numbers. The format is,

logspace(RangeStartingfrom 10^THISNUMBER, RangingEndingAt 10^THISNUMBER, numberOfLogarithmicallyPoints)

So, logspace(1, 5, 10); will mean that we wish to create a logarithmically spaced vector of 10 points between 10^1 and 10^5

This just about covers the basics of Matrix and vectors. You have learnt the basics and construction of some of the most commonly used matrices in Matlab. Now, you have enough understanding to proceed towards matrix operations and manipulation.

Return to Matlab Tutorials

Variables, Arithmetic operations and Functions in Matlab

Return to Matlab Tutorials

In this tutorial I will explain to you how to use variables, perform basic arithmetic operations and usage of some common mathematical functions in Matlab. Lets start right away.

Creating a simple variable

Creating a variable is very simple. A variable name, an “=” sign and a value. For example, in command window, you would write,

x = 100

Here, we defined a variable x to have a value of 100.

Basic Arithmetic Operations

Addition, subtraction, multiplication and division is performed by using +, -, *, / operators respectively on a variable. This is shown below:

Powers

We are going to give power to variables now. It is simple like:

>> x = 4

x =4

>> x^1000

ans =Inf

>> x^100

ans =1.6069e+60

Squareroot

Root the numbers back to their squares using:

sqrt(x);

sqrt(16);

Logarithmic Operations

This is done like:

log(105);

log10(105);

log10(pi);

log(-1);

Trigonometric Ratios

You do it like a boss:

boss = pi;

sin(4*boss/3);

tan(boss*boss);

 

There are much more functions to get you started on your work in Matlab. But this is enough for this tutorial as I will slowly be advancing towards more advanced and specialized tutorials in the future.

Do comment if you have questions.

Return to Matlab Tutorials

Matlab Command Windows Basic Operations

Return to Matlab Tutorials

Command line works similar to our normal handheld calculator.  You gave it an Input and it performs operations. Also, you can tell you to perform certain actions!

These are some of the actions that you need to learn because they will be used repeatedly.

One interesting thing to note is that you can chose to include or omit the use of semicolon 

Inclusion of ; will not display the result of command in command window

Omission of ; will surely display the result of command.

Result could be either a variable value, matrix, graph, image or a video.

disp(‘Hello World!’)

You guessed it right! It shows the world famous Hello World of Matlab.

clc

This command clears the command window. It does no harm to the variables and values saved in your workspace. Instead, it simply clears the window for you. Similar to clearing your desk space while your books are safe in the book shelf ?

Clear

This sucker clears all the variables that you have saved in the workspace. Use carefully as it has messed up many people’s progress.

 Clf

So you have 100 fancy plots made using various scripts and you want to clear the plots? This is where it comes handy. Run clf in command window and all the plots will be cleared.

 close all

This closes all the windows that you have opened using your program in Matlab. For example, it will close all plots, images, graphs, etc.

 

doc <function>

This is the best feature of Matlab. Lets suppose you want to know what the frigging “:”  does in Matlab. Simply type doc : in matlab and it will show you help about it.

% (used for comments)

For example, “% clear all” will  make your program ignore that line of code. Best used using Shortcut CTRL+R.

format short and format long

If you wish to change the command window output to smaller or longer decimal places, you can use this command like this.

 

Return to Matlab Tutorials

MATLAB Tutorial 1 – Introduction

Who cares what genre of sorcery MATLAB belongs to, you are here to learn it and this alone proves the fact that you know what it does and doesn’t.  But it won’t hurt to shed some light into its basics:

What is the purpose of MATLAB?

MATLAB stands for Matrix Laboratory. First released on 1984 :S. Runs on Windows, Linux, OS X. Has 32-bit and 64-bit support. The data type used generally is a Matrix. MATLAB helps you to perform numerical computing.

Why learn/use MATLAB?

  1. Research
  2. PhD/MSc/Academic Purpose
  3. Rapid prototyping
  4. Engineering Simulation
  5. Algorithm Development
  6. Analysis of data
  7. Fast Visualization of data
  8. Huge multidimensional arrays
  9. Data Proccessing
  10. Frying the CPU
  11. Image Processing
  12. Cracking a nut open
  13. A SUPERDUPERHIFI Calculator
  14. And much more!!

Why MATLAB hurts?

  1. Devours a large chunk off your computer’s memory.
  2. If you have a slow computer, get ready to face long processing times.
  3. When you perform a large calculation/analysis, MATLAB tends to take maximum performance that Windows can provide it. Very Greedy!

Lets start learning 🙂

Return to Tutorials List