You are on page 1of 45

Presentation

Introduction to Matlab

1
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
2
The Desktop

3
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
4
Defining a Variable

For assigning a value to a variable:


x=4

Now type the following command in the command


window:
a = 45;

5
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
6
Defining a Vector

For storing an array, or vector, you can use either of


the following two commands:

x = [1 2 3 4 5 6] (Separated by space)

OR

y = [1,2,3,4,5,6] (Separated by Commas)

7
Defining a Vector

8
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
9
The Colon [:] notation

Another method to enter the above array is by using


the colon notation:

x = 1:6

10
The Colon [:] notation

And if we want to change the increment value to


say, 4, we’ll use:

x = 1:4:20

So, if we want to use a decrementing factor, simply


use a negative sign. Here:

x = 99:-1:88

11
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
12
Accessing Elements of a Vector

If you want to access the, say 4th element of an


array, you’ll have to use the command:

x(4)

Remember, the indexing of elements of an array start


at 1, not 0 in MATLAB.

For accessing elements from 2nd to 7th element for


example, use:

x(2:7) (Accessing odd indexed elements?)


13
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
14
Size and Length of a Vector

The ‘size’ command gives the number of rows and


columns of a matrix, and the command ‘length’ gives
the length of the vector.

size(x)
ans =
1     6
Try, [r,c] = size(x)
length(x)
ans =
6

15
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
16
Well Known Vectors

Explore the MATLAB help for following:

Try the following commands in the Command


Window:

help ones
help zeros
help linspace

17
Well Known Vectors

Example:

xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ]

This code is first assigning three zeros to xx


using zeros(1,3)
— linspace(0, 1, 5) is generating 5 points
between 0 and 1
— and at the end ones(1,4) is assigning four
ones to xx.

18
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
19
Vector Operations

To multiply a scalar with an array.

Here’s an example:

tpi = pi * [ 0:0.1:2 ]

This code will assign tpi, multiples of pie from 0 to 2


incrementing by 0.1

20
Vector Operations

To Add and Subtract Two Vectors

If you want to add or subtract two vectors, you have


to make sure that the length of both vectors is same!

x – y;

x+y;

21
Vector Operations

Multiplication and Division of Two Vectors

For the case of multiplication and division, the


dimensions of the vectors must be same.

x/y;
Slash or matrix right division. B/A is roughly the same
as B*inv(A)

x*y;
C = A*B is the linear algebraic product of the
matrices A and B
22
Vector Operations

Dot Multiplication and Division

Use of the ‘.*’ or ‘./’ notation to multiply or


divide.

x.*y;

x./y;

This command will divide or multiply the 1st element


of x by 1st element of y, 2nd element of x by 2nd
element of y and so on.
23
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
24
Matrices

To enter a matrix use the following command:

A = [1 2 3;4 5 6]

25
Arithmetic Operators

+ addition
- subtraction
* multiplication
/ division
^ power
‘ complex conjugate transpose
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
.‘ transpose

26
Logical Operators

== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
& AND
| OR
~ NOT

27
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
28
Matlab Graphics

A simple example for the ‘plot’ command:

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function')

29
Matlab Graphics

30
Matlab Graphics

Multiple Graphs:

t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on

31
Matlab Graphics

Graph Functions:

plot linear plot


stem discrete plot
grid add grid lines
xlabel add X-axis label
ylabel add Y-axis label
title add graph title
subplot divide figure window
figure create new figure window

32
M-files and Functions

function result = test(input)

x = mod(input,2);

if x == 0
result = 'even';
else
result = 'odd';
end

33
Flow Control

• if statement
• switch statement

• for loops
• while loops

• continue statement
• break statement

34
Overview.

 The Desktop
 Defining a Variable
 Defining a Vector
 The Colon Notation
 Accessing Elements of a Vector
 Size and Length of a Vector
 Well Known Vectors
 Vector Operations
 Matrices
 Matlab Graphics
 Sounds and Images
35
Sounds

x = wavread('911.wav');

• wavwrite
• auread
• auwrite
• soundsc

36
Images

>> x = imread('cameraman.tif');
>> imshow(x)

37
Images

Each image is composed of an array of M*N


pixels (contraction of “picture element”) with M
rows and N columns of pixels.

Each pixel contains a certain value for red,


green and blue.

Varying these values for red, green, blue (RGB)


we can get almost any color.

38
Images

39
Images

Separating the color components of an RGP


image:

%% Input Image
pic = imread('images.jpeg');
imshow(pic), title('Original Image')
Original Image

40
Images

%% Red Component
r_pic = pic(:,:,1);
figure, imshow(r_pic), title('Red Component of
the Image')
Red Component of the Image

41
Images

%% Green Component
r_pic = pic(:,:,2);
figure, imshow(r_pic), title('Green Component of
the Image')
Green Component of the Image

42
Images

%% Blue Component
r_pic = pic(:,:,3);
figure, imshow(r_pic), title('Blue Component of
the Image')
Blue Component of the Image

43
Images

%% Accessing a Pixel
check = x(67,38,:)

check(:,:,1) =
252

check(:,:,2) =
255

check(:,:,3) =
255

44
Thank you for your patience!

45

You might also like