Professional Documents
Culture Documents
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
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
x = [1 2 3 4 5 6] (Separated by space)
OR
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
x = 1:6
10
The Colon [:] notation
x = 1:4:20
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
x(4)
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
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
help ones
help zeros
help linspace
17
Well Known Vectors
Example:
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
Here’s an example:
tpi = pi * [ 0:0.1:2 ]
20
Vector Operations
x – y;
x+y;
21
Vector Operations
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
x.*y;
x./y;
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
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
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:
32
M-files and Functions
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
38
Images
39
Images
%% 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