DIGITAL IMAGE
PROCESSING IN
MATLAB
LECTURE 4
3D Line Plots
Plot(1, 1, ‘ro’)
Plot3(1, 1, 1, ‘ro’)
Exercise
Z=a row vector from 0 to 4*pi in 0.001 increments.
X=sin(z)
Y=cos(z)
Plot these in 3D
Plot3(x, y, z)
15
10
0
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1
How to remove specific rows from your matrix?
A=[ 1 2 4;
5 6 7;
8 9 0;
2 6 9;];
A_r=removerows(A, [1, 3])
A_r = ROWS 1
AND 3
NOW
5 6 7 REMOVED
2 6 9 FROM A!
Or you can use a manual way…
A=[ 1 2 4; Row 1 to row 3 and all
corresponding columns made
5 6 7; empty----hence removed!
8 9 0;
2 6 9;];
A( [1, 3] , : )=[ ]
A=
5 6 7 EXACT
SAME
2 6 9 RESULT!
To remove columns
Use the manual way like you removed rows
Set the column you want to remove to be equal to an
empty matrix [ ].
Magic square
It is one of the most interesting square matrices that
you will find
>>magic(3)
8 1 6
3 5 7
4 9 2
Can you figure out what is so special about it?
Magic square
>>magic (n)
This command will give you an nxn matrix of
integers
In this matrix, minimum integer=n and maximum
integer=n^2
Sum of any row = sum of any column = constant
Magic(3)
SUM=15
8 1 6
SUM=15
3 5 7
SUM=15
4 9 2
SUM=15 SUM=15 SUM=15
What exactly is so magical about this matrix?
Matrix1=magic(19);
Plot(Matrix1)
Magic(19)
400
350
300
250
200
150
100
50
0
0 2 4 6 8 10 12 14 16 18 20
Similarly magic(20) gives…..
400
350
300
250
200
150
100
50
0
0 2 4 6 8 10 12 14 16 18 20
Exercise
Make a magic matrix of n=5
Remove its row 1and 2 and columns 3 and 4
Solution
m=magic(5)
mm=removerows(m, [1, 2])
mm(:, 4:5)=[]
Switch statements- a substitute of if else statements
Suppose in your code, you have twenty different if
else conditions.
It would be very confusing to list them all down
while coding- not to mention the headache it will
give the reader who is trying to understand your code
Often, people will use an “If else if” statement where
a “Switch case” statement is going to be cleaner and
easier to understand and maintain.
Switch statements
It allows you to branch among several cases just as
easily as between two cases, though the cases must
be described through equalities rather than
inequalities. Here is a simple example
A comparison…….
Using Strings with switch…
An example
X= input('enter a number:');
switch X
case 1
disp ('one');
case 2
THE ‘OTHERWISE’ COMMAND
disp ('two'); IS NOT NECESSARY….. JUST
LIKE AN ‘ELSE’ COMMAND IS
otherwise NOT ALWAYS NECESSARY
WITH AN ‘IF’ COMMAND
disp ('other');
end
Functions
Suppose there are a few lines of code that you need to
execute again and again in your program. Instead of
repeating those same lines again and again in your
script, a better practice would be to write them once
in a separate (function) file and whenever those lines
need to be executed, simply call that function.
A function is a group of statements that together
perform a task. In MATLAB, functions are defined in
separate files. The name of the file and of the function
should be the same.
Basic types of functions
No input, no output.
Input(s), but no output.
No input but output(s).
No input, no output
function [ ]=hh( )
disp(‘I am a function')
End
Call this function from your script or command
window using this command:
>>hh
Input(s), but no output
function [ ]=my_func(a, b)
c=a+b;
Disp( c )
End
Call this function from your script or command window
using this command:
>>hh(f, g) %’f’ gets treated as ‘a’ and ‘g’ gets treated
as ‘b’ in the function
No input but output(s)
function [c]=my_func(a, b)
c=a+b
End
Call this function from your script or command window using this
command:
>> p=my_func (g, k)
NOTE:
If there are multiple outputs p, q and u, call your function like this:
>> [p q u]=my_func (g, k)
Exercise
Write a script. That script will take two input
integers from user.
Send them to a function that checks which integer
is greater, and it will return that integer.
Nested Loops
MATLAB allows to use one loop inside another
loop.
Nested for loop
Example
Make any 5x4 matrix and display all its elements
using nested for loop.
Nested while loop
Example
Take a user input number (which should be less
than or equal to 10) five times. If the user inputs a
number greater than 10, tell the user that this
number is invalid and that he should enter again.
Loop Control Statements
break statement: Terminates the loop statement and
transfers execution to the statement immediately
following the loop.
Continue statement: Causes the loop to skip the
remainder of its body and immediately retest its
condition prior to reiterating.
Break command
for i=[Link]
if i==3
break;
end
disp(i)
end
Continue command
for i=[Link]
if i==3
continue;
end
disp(i)
end
Remember this task from your prev assignment?
Write a program that displays dice throw result (integer
between 1 and 6). After displaying that integer, the program
should ask the user if he wants to end the program.
If the user presses ‘y’ or ‘Y’, the program should end.
If the user presses ‘n’ or ‘N’, the program should display
another dice throw integer. After displaying that integer, the
program should ask again if the user wants to end the
program (and the cycle repeats).
If a user enters any other character string then the program
should display ‘wrong entry, enter again’ and take the input
again.
Nested While and break command will make this
much more simple!!!
Debugging your program
for i=[Link]
if i==3
continue;
end
if i==4
break;
end
disp(i)
disp('done that')
end
Continuation marks
continue a statement to the next line using ellipsis
(….)
g=[1 2 3....
4 5 6]
Is the same as
g=[1 2 3 4 5 6]
Continuation
For strings:
g=['hello, I am....
Kate']
THIS WILL GIVE AN ERROR!
The string must end on the same line that it started on, you
can define different strings in new line though.
g=['hello, I am '....
'Kate']
Gives
g = ‘hello, I am Kate’
Some other tips
Vec=[a b c] and Vec=[a, b, c] both commands
create a row vector.
Vec=[a; b; c] and Vec=[a (enter) b(enter) c] both
commands create a column vector.