In this article, we are giving an introduction to MATLAB. This article is part of a tutorial series we are developing for MATLAB. We hope you to give us feedback, so that we can improve our MATLAB tutorial series.

### What does MATLAB stand for?

MATLAB stands for **MAT**rix **LAB**oratory. Hence, as the name suggests, here you play around with matrices. Â Using MATLAB an image (or any other data like sound, etc.) can be converted to a matrix and then various operations can be performed on it to get the desired results and values. Image processing is quite a vast field to deal with. We can identify colors, intensity, edges, texture or pattern in an image. In this tutorial, we would be restricting ourselves to detecting colours (using RGB values) only.

### Getting acquainted with MATLAB environment

For those who have just finished installing MATLAB on their system and canâ€™t figure out from where to start, no need to worry! This tutorial will first make you well acquainted with its very basics and then move further.

So, a typical MATLAB 2009 window looks like the image shown below.

As marked in the image, there are 4 main windows.

**Command window:** This is the main window where you write the commands, as well as see the outputs. In other words, here is your interaction with the software.

**Command History:** As the name suggests, it shows the list of the commands recently used in chronological order. Hence, you can double click on a command to execute it again.

**Current directory:** It is the default directory (folder) for saving your files. All the files which you make (like m-files, as discussed later) are saved here and can be accessed from here directly. The location of the current directory is shown in the toolbar at the top. You can change it by changing the address here.

**Workspace:** It displays the list of the variables defined by you in the current session of MATLAB.

Â **The Menu bar and Toolbar:Â **The Â toolbar Â has Â buttons Â for Â common Â operations Â like Â cut,Â copy, Â paste, Â undo, Â redo. Â The Â most important button here is the HELP button. It opens the MATLAB help window which Â looks as the screenshot given below.

Â You can Â get Â the Â details Â of Â any Â MATLAB Â command/function Â here Â and Â many Â demos Â ofÂ some commonly used applications. Locate the four tabs: Contents, Index, Search Results and Demos on the left. One of the best ways to learn by yourself is to look for demos of your interest and type the command/ function which you encounter there as the search term. You will then get the complete details of the function like its use, syntax, as well as few examples on how to use it. You can also have a look at some of the related functions at the end of page under the heading â€œSee Alsoâ€. The demos related to Image Processing can be found under Image Processing Toolbox and Image Acquisition Toolbox.

Now once we are done with knowing the essential features of MATLAB, letâ€™s start typing something in the command window, say: a=5 and press enter.

Yesâ€¦ as you can see that MATLAB creates a variable with name â€˜aâ€™, stores value 5 in it and displays it in the command window itself. Hence you can see how user-friendly MATLAB is.

Variables are stored in the form of Matrices in MATLAB. Hence, â€˜aâ€™ is a 1X1 matrix in the above example. Similarly, you can make one dimensional, two dimensional, etc. matrices as follows:

>> a=[1 3 5 7 9]

a =

1Â Â Â Â 3Â Â Â Â 5Â Â Â Â 7Â Â Â Â 9

>> b=[1 2 3;4 5 6;7 8 9]

b =

1Â Â Â Â 2Â Â Â Â 3

4Â Â Â Â 5Â Â Â Â 6

7Â Â Â Â 8Â Â Â Â 9

To avoid the display of the variable, we use semi-colon (;) at the end of instruction.

Â >> b=[1 2 3;4 5 6;7 8 9];

The indices of matrices in MATLAB start from 1 (unlike C/C++ and Java where they start from 0). We refer to a particular element of the matrix by giving its indices in parenthesis ().

>> Â b(2,1)

ans =4

Now Â with Â the Â variables Â in Â hand, Â you Â can Â perform Â various Â mathematical Â operations Â on Â them directly.

Â >> a=[1 2 3];

>> b=[6 7 8];

>> a+b

ans =

7Â Â Â Â 9Â Â Â 11

ans is the default variable of MATLAB. You can also store the result in another variable as

Â >> c=a+b c =

7Â Â Â Â 9Â Â Â 11

### General functions & commands in MATLAB

**clc: **To clear the command window, giving you a â€˜clear screenâ€™.

**clear: **To remove all variables from the workspace. This frees up system memory.

### Trigonometric functions

For angle in radians-

Â >> sin(1)

ans =0.8415

For angle in degrees

>> sind(30)

ans =0.5000

Inverse trigonometric-

Â >> asin(1)

ans =1.5708

>> asind(.5)

ans =30.0000

Similarly we have cos(), cosd(), acos(), tan() and other functions.

### The Colon Operator

The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays, andÂ specify Â for Â iterations. Â In Â aÂ very Â crude Â language, Â we Â can Â say Â that Â the Â colon Â (:) Â means â€œthroughout the rangeâ€.

j:kÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â is the same as [j,j+1,…,k]

j:i:kÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â is the same as [j,j+i,j+2i, …,k] A(:,j)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â is the jth column of A

A(:,j:k)Â Â Â Â Â Â Â Â Â Â Â Â Â Â is A(:,j), A(:,j+1),…,A(:,k)

A(:)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â is all the elements of A, regarded as a single column.

**Example:Â **

>> a= [1 2 3; 4 5 6; 7 8 9]

a =

1Â Â Â Â 2Â Â Â Â 3

4Â Â Â Â 5Â Â Â Â 6

7Â Â Â Â 8Â Â Â Â 9

>> a(:,2:3)

ans =

2Â Â Â Â 3

5Â Â Â Â 6

8Â Â Â Â 9

### Relational Operators

Operator |
Description |

== |
Equal to |

~= |
Not equal to |

< |
Less than |

<= |
Less than or equal to |

> |
More than |

>= |
More than or equal to |

### Frequently used Functions & Commands

#### Conditional Statements

**if****, else: **Execute statements if condition is true, false respectively.

**Syntax:**

if condition1 statement

elseif condition 2 statement

else

statement end

**Â Example:**

a=10;

>> if a<10 b=a/2;

else

b=a*2;

end

>> b

b =20

*Note: Â As Â a Â block Â is Â contained Â in braces Â {} Â in Â C/C++/Java, Â a block Â is Â terminated Â by Â the Â â€˜endâ€™statement in MATLAB.

#### LOOP-ing Commands

**fo****r****: **To create a loop – that is to execute a block of code specified number of times.

**Syntax:**

for variable = initval:endval statement

… statement

end

**ExampleÂ **

>>c=[1 2 3 4 5]; b=0;

>>for i=1:5 b=b+c(i); end

>> b

b =15

**wh****il****e: **Again to create loop, that executes till a specified condition is true.

**Syntax:**

while condition statements

end

**Â Example:**

>> c=2009; i=1; while c>1 b(i)=mod(c,10); c=c/10; i=i+1; end

>> b

b =9.0000Â Â Â 0.9000Â Â Â 0.0900Â Â Â 2.0090

**z****e****r****o****s(): **Create array/matrix of all zeros.

B = zeros(n) returns an n-by-n matrix of zeros.

B = zeros(m,n) returns an m-by-n matrix of zeros. Example,

>> z=zeros(2,4)

z =

0 0

0

0 0 0

0

0

Similarly we have ones() function for all values 1.

**size(): **Returns matrix dimensions.

**Example:**

for the above matrix z,

>> size(z)

ans =

2Â Â Â Â 4

**l****ength(): **Returns the length of a vector. For an array, it returns the size of the longest dimension.

**Example:**

>>x = ones(1,8);

>>n = length(x)

n =8

**d****ot****(): **Returns dot product of two vectors.

Example:

C = dot(A,B)

**sqrt(): **Returns square root of each element of an array

**m****i****n****(): **Returns smallest elements in an array.

**Syntax:**

C = min(A)

If A is a matrix, min(A) treats the columns of A as vectors, returning a row vector containing the minimum element from each column. Similarly we have **max() **function.

**sort()**: Sorts array elements in ascending or descending order

**Â Syntax:**

B=sort(A,mode)

where value of mode can be

‘ascend’ Â :Â Â Â Â Â Â Â Â Â Â Ascending order (default)

‘descend’ :Â Â Â Â Â Â Â Â Â Â Descending order

**p****l****o****t****(): **Creates a 2-D line plot

So these all are the frequently used functions/commands in MATLAB. Until we come with our next chapter in MATLAB tutorial series, please share this article with your friends. Give us your feedback on improving the tutorial as well, we value it!

## Comments