Anda di halaman 1dari 13

EELE 5110 Digital Image Processing Lab.

Lab. 1 Introduction to Matlab

1. What Is MATLAB?
MATLAB is a high-performance language for technical computing. It integrates
computation, visualization, and programming in an easy-to-use environment where
problems and solutions are expressed in familiar mathematical notation. Typical uses
include:

Math and computation.


Algorithm development.
Data acquisition.
Modeling, simulation, and prototyping.
Data analysis, exploration, and visualization.
Scientific and engineering graphics.
Application development, including graphical user interface building.

2. Basic Operations:
2.1 Computation
% This is an example of arithmetic
>> 1+2
Ans =
3

2.2 Suppressing Display of Results


>> 1+2;

2.3 The workspace


Current values are stored in the MATLAB workspace. The 'who' command tells
you what variable are currently defined. The 'whos' command tells you more abut
the variables. Use the 'clear' command to delete variables from the workspace.
>> x=2;
>> y=3;
>> z=x+y
z =
5
>> who
Your variables are:

>> whos
Name
x
y
z

Size
1x1
1x1
1x1

Bytes
8
8
8

Class
double array
double array
double array

Grand total is 3 elements using 24 bytes


>>

2.4 Command Line Editing and Recall:

MATLAB does not work like a spreadsheet. It will not go back and repeat
calculations unless you tell it to.
A comma ( , ) separates statements on a line.
Three periods ( ) continues on the next line.
The up arrow ( ) goes to the previous command.
The down arrow ( ) goes to the next command.
The sideways arrows move within the command.
MATLAB is case sensitive.

2.5 MATLAB Operations and Conventions:

Expression follow the standard of precedence


Exponentiation.
Multiplication and Division.
Addition and Subtraction.
Expressions are evaluated from left to right.
Paratheses work from inner to outer.
Complex numbers are entered using the characters ' i ' and ' j ', unless
other definitions have been assigned to them.

2.6 To ask for online help with a command, type ' help ' followed by the
command name. For instance
>> help cos
COS
Cosine.
COS(X) is the cosine of the elements of X.

3. Matrices:
MATLAB works essentially with only one kind of object a rectangular numerical
matrix with possibly complex entries. All variables represent matrices. A scalar is a
1x1 matrix and a vector is a matrix with only one row or column. There are several
ways to enter matrices into MATLAB including
Entering an explicit list of elements
Using built-in statement or function.
Created in a diskfile with your local editor.

Loaded from external data files or applications

3.1 Generating Vectors and the Colon Notation


We can create a vector (matrix with only one row) by directly inputting the elements
>> W=[1 2 3 4]
W =
1

We could also from a vector by using the colon ( : ) operator . " Colon Notation " is
used to generate vectors and to reference submatrices. The colon notation and
subscripting by integral vectors and keys to efficient manipulation in MATLAB.
Creative use of these features to factorize operations lets you minimize the use of
loops (which slows MATLAB down) and makes code simple and readable.
>> Q=[1:5]
Q =
1

We could also use any increment to construct matrices


>> E=[2:2:10]
E =
2

10

3.2 Generating Matrices:


Column matrices can be created with the semicolon ( ; )
>> A=[1,2,3;4,5,6;7,8,9]
A =
1
4
7

2
5
8

3
6
9

3.3 Referencing Individual Entries:


Round parentheses are used to reference individual elements of a matrix
>> A(1,2)
ans =
2

3.4 The colon notation can be used to access submatrices of a matrix.

A(1:4,3) is the column vector consisting of the first 4 entries of the 3rd column of

matrix A.
3.5 Deleting Rows and Columns
You can delete rows and columns from a matrix using just a pair of square brackets.
Start with
>> X=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
X =
1
5
9
13

2
6
10
14

3
7
11
15

4
8
12
16

>> %To delete the second column of X


>> X(:,2)=[]
X =
1
5
9
13

3
7
11
15

4
8
12
16

4. Matrix Operations
The following matrix operation are available
+
addition
subtraction
*
multiplication
^
power
'
transpose ( real ) or conjugate transpose ( complex )
.'
transpose ( real or complex )
\
left division
/
right division
4.1 Matrix Division
If A is an invertible square matrix and b is a compatible column vector, or
respectively a compatible row vector, then
x=A\b is the solution of A*x = b
x= b/A is the solution of x*A=b
4.2 Entry-Wise Operations
The matrix operations addition and subtraction are already entry-wise but the
other operations are not; they are matrix operations. The other operation,*,^,\,/ can be
made to operate entry-wise by preceding them with a period.
>> B=[1 2;3 4]

B =
1
3

2
4

>> B*B
ans =
7
15

10
22

>> B.*B
ans =
1
9

4
16

5. Matrix Building Functions


Indentity matrix
Matrix of zeros
Matrix of ones
Upper triangular part of matrix
Lower triangular part of matrix
Matrix with random elements

Eye
Zeros
Ones
Triu
Tril
Rand

For example,
>> zeros(2,3)
ans =
0
0

0
0

0
0

>> A=rand(3)
A =
0.9501
0.2311
0.6068

0.4860
0.8913
0.7621

0.4565
0.0185
0.8214

0.4860
0.8913
0

0.4565
0.0185
0.8214

>> triu(A)
ans =
0.9501
0
0

Matrices can be built from blocks, for instance try


A=rand(3);
B=[A, zeros(3,2); zeros(2,3),eye(2)];

6. Complex number
Entering complex numbers from the keyboard has to be done carefully. The
symbol "i" identifies the imaginary part and has to be typed immediately after the
numerical value of the imaginary part: for example, 2+3i . If you insert a space-for
instance, 2+3 i, it looks like the same expression but it will be processed as a number (
2+3 ) and a string ( i ), and not as the complex number ( 2+3i ) .
It is also important to point out that termination with the character i only
works with simple number, not expressions. For example, the expression ( 1-2i )i has
no meaning to MATLAB. If you want to multiply a complex expression by i, you
have to use the multiplication operation symbol (*) . In the example above , you must
write (1-2i)*i. Similarly, the number 1-sin(2)i has no meaning for MATLAB. It has to
be written as 1-sin(2)*i to make sense to the program.
Note: you can use j instead of i.

6.1
Function for Complex Numbers:
Command
This return the
Complex(x,y)
Return a complex number x+yi
Real(x)
Real part of a complex number x
imag(x)
Imaginary part of a complex number x
Abs(x)
Magnitude f the complex number x
angle(x)
Angle of a complex number x
conj(x)
Complex conjugate of the complex number x
Cart2pol
Convert Cartesian of Polar from of complex number
Pol2cart
Convert Polar of Cartesian from of complex number
7. Control Flow Statements
7.1

Relations

The relational operators in MATLAB are:


<
>
<=
>=
==
~=

less than
greater than
less than or equal
greater than or equal
equal
not equal

Note that '=' is a direct assignment while ' == ' is the logical equal. Relations may be
connected or quantified by the logical operators
&
|
~

and
or
not

7.2

Variable Controlled Loops ( for)

The general form is


for variable = first : inc : last
statements
end

If the increment ' inc ' is not specified, a default value of 1 is used. For example
>> x=[];
>> for i=1:4
x=[x,i^2]
end
x =
1

x =
1

x =

x =
16

7.3
Relational Controlled Loops (while)
The general form is
while relation
statements
end

For instance
i=0;
while i<3
i=i+1
end
i =
1
i =
2
i =
3

A ' while ' loop is useful when, in contrast to a ' for ' loop, the calculations are to be
repeated an undetermined number of times.
7.4

Branching ( if )

The general form is,


if relation
true alternative
else
false alternative
end
if 1>2
a=1
else
a=2
end
a =
2

7.5

Switch:

MATLAB includes a switch structure .The general form is


Switch variable
Case value 1
Statement group 1
Case value 2
Statement group 2
..
Otherwise
Last statement group
End

For example, try the following script M-file


angle = 75;
switch angle
case 0
disp('East')
case 90
disp('North')
case 180
disp('west')
case 270
disp('South')
otherwise
disp('lost')
end
switchangle
lost

7.6

Break and Return:

The 'break' command causes the enclosed loop either ' for ' or ' while ' loop
to terminate.
The ' return ' command causes the currently executing M-file to terminate.
8. plotting:
8.1 Basic Two-Dimensional Plot
MATLAB has extensive plotting capabilities. We will examine a simple twodimensional plot and add features. The ' plot ' command graphs the numbers in
one array versus the numbers in a second array of the same length. For example,
t=0:0.01:2;
temp=exp(-t);
plot (t,temp)
xlabel('Time')
ylabel('Temp')
title('Transient Temperatures')

8.2 Colors, Symbols and lines Types


You can change colors, symbols and line types as demonstrated below
x=0:pi/16:2*pi;
y=sin(x);

plot(x,y,'*..')
xlabel('x')
ylabel('sin(x)')

8.3 Multiple Plots on a Single Graph


x=0:pi/16:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'* -',x,y2,'r s -')
xlabel('x')
ylabel('sin(x) , cos(x)')
title('Trig Functions')
legend ('sin','cos')

8.4 Subplots
You can create graphics arrays using the ' subplot ' command.
x=0:pi/16:2*pi;
y1=sin(x);
y2=cos(x);
subplot(2,1,1)
plot(x,y1,'* -')
xlabel('x')
ylabel('sin(x)')
subplot(2,1,2)
plot(x,y2,'r s -')
xlabel('x')
ylabel('cos(x)')

8.5 Function of plot


You may use the following function; try to find to its function:
Hold, Grid, Axis, Ezplot, Gtext, Ginput

Exercises:
1. Perform the matrix products in MATLAB:
a.

b.

2. A nice example of a function that would be difficult to graph without a


computer or calculator is
F(x) = x( x x ) (xx)x
Plot f(x). ( Hint: - By itself , ezplot uses the rang -2 x 2 . This is
inappropriate here : a better choice is to try the range 0 x 2 ).
3. Obtain plots of the following using plot function:

Y(t)= 1-2e-t sin (2-35 ) , 0 t 10


4. A vector x has been obtained from measurements. Suppose we want to
consider any data value in the range -0.1<x<0.1 as bearing erroneous we
want tot remove all such elements and replace r=them with zeros at the end
of the array. Develop two ways of doing this. An example is given in the
following table.
(Hint: use function find, length)

X(1)
X(2)
X(3)
X(4)
X(5)
X(6)
X(7)

Before
1.92
0.05
-2.43
-0.02
0.09
0.85
-0.06

After
1.92
-2.43
0.85
0
0
0
0

Anda mungkin juga menyukai