Dip - Lab Manual - 1 - Updated
Dip - Lab Manual - 1 - Updated
LAB MANUAL 1
Workspace
Command
History
• clc Command: Clears the Command window and homes the cursor.
• help Command: help <Topic> displays help about that Topic if it exist.
• lookfor Command: Provides help by searching through all the first lines of
MATLAB help topics and returning those that contains a key word you specify.
• edit Command: enable you to edit (open) any M-file in Editor Window. This
command doesn’t open built-in function like, sqrt. See also type Command.
• more command: more on enables paging of the output in the MATLAB
command window, and more off disables paging of the output in the
MATLAB command window.
Notes:
• A semicolon " ; " at the end of a MATLAB statement suppresses printing of results.
• If a statement does not fit on one line, use " . . . ", followed by Enter to indicate that the
statement continues on the next line. For example:
>> S=sqrt (225)*30*...
(20*sqrt (100))
• If we don’t specify an output variable, MATLAB uses the variable ans (short for answer), to
store the last results of a calculation.
• Use Up arrow and Down arrow to edit previous commands you entered in Command Window.
• Insert " % " before the statement that you want to use it as comment; the statement will appear
in green color.
Example 1:
>> a=3
>> a=3; % can you see the effect of semicolon " ; "
>> a+5 % assign the sum of a and 5 to ans
>> b=a+5 % assign the sum of a and 5 to b
>> clear a
>> a % can you see the effect of clear command
>> clc % clean the screen
This is the basic introduction to Matlab. Creation of vectors is included with a few basic
operations. Topics include the following:
1. Defining a vector
2. Accessing elements within a vector
>> v = [3 1 7 -21 5 6 4]
v=
3 1 7 -21 5 6 4
>>v
v=
3 1 7 -21 5 6 4
Notice:
though, that this always creates a row vector.
If you want to create a column vector you need to take the transpose of a row vector.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
v=
3
1
7
-21
5
6
4
>> w=v.’
w=
3
1
7
-21
5
6
4
>>v(1:3)
ans=
3 1 7
>>v(2:4)
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
ans=
1 7 -21
Similarly to access all elements from some specific location say the 3rd through the last
element
>>v(3:end)
ans=
7 -21 5 6 4
>>v(:)
>>v(1:end)
ans=
4 5 7 3
A common task is to create a large vector with numbers that fit a repetitive pattern.
Matlab can define a set of numbers with a common increment using colons.
For example, to define a vector whose first entry is 1, the second entry is 2, the third is
three, up to 8 you enter the following:
>> v = [1:8]
v=
1 2 3 4 5 6 7 8
If you wish to use an increment other than one that you have to define the start number,
the value of the increment, and the last number.
For example, to define a vector that starts with 2 and ends in 4 with steps of .25 you enter
the following:
>> v = [2:.25:4]
v=
Columns 1 through 7
Columns 8 through 9
3.7500 4.0000
For example to view the first entry just type in the following:
>>v(1)
ans =
This command prints out entry 1 in the vector. Also notice that a new variable called ans has
been created. Any time you perform an action that does not include an assignment MATLAB
will put the label ans on the result.
A vector can be used as an index into another vector.
For example we can pick the first, third sixth and seventh elements of v using the command
>>v = [3 1 7 -21 5 6 4]
>>v([1 3 6 7])
v=
3 7 6 4
Defining Matrices
Defining a matrix is similar to defining a vector.
To define a matrix, you can treat it like a column of row vectors (note that the spaces are
required!):
>> A = [ 1 2 3; 3 4 5; 6 7 8]
A=
1 2 3
3 4 5
6 7 8
B=
1 2 3
2 4 5
3 7 8
If you lose track of what variables you have defined, the whos command will let you know
all of the variables you have in your work space.
>>whos
Name Size Bytes Class
A 3x3 72 double array
B 3x3 72 double array
v 1x5 40 double array
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
You can work with different parts of a matrix, just as you can with vectors. Again, you have to be
careful to make sure that the operation is legal.
>> A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(2,3)
ans = 5
The colon operator is used in the matrix indexing to select a two dimensional block of
elements out of matrix:
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>C= A( : , 3) % this statement picks the third column of the matrix
ans=
C=
3
5
8
The colon operator can also be used in the matrix indexing to extract the row of the
matrix
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>> C= A (2 , :) % this statement picks the 2nd row of the matrix
ans=
C=2 4 5
Slicing matrix A
>>A = [ 1 2 3; 3 4 5; 6 7 8]
>>A(1:2,2:3)
A=
1 2 3
3 4 5
6 7 8
ans =
2 3
4 5
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(1:2,2:3)'
ans =
2 4
3 5
To create a matrix B equal to A but with its last column set to 0’s we write
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>B= A;
>> B (: , 3)=0
B=
1 2 0
2 4 0
3 7 0
>> A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(end , end)
ans =
8
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>D= A ([1 3] , [2 3])
D=
2 3
7 8
The use of a colon is useful in case to find the sum of all elements of a matrix
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>s= sum (A(:))
s =39
A = [ 1 2 3; 3 4 5; 6 7 8]
>> A = 5 * ones (3,3)
A=
5 5 5
5 5 5
5 5 5
magic (n ) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal
row and column sums.
The order n must be a scalar greater than or equal to 3 in order to create a valid magic square.
Command Description
>>x=[ 1 2 3; 3 4 5; 6 7 8]
>> size(x)
ans = 3 3
single quote ( ' ) Matrix transpose flips a matrix about its main
>> x=[1 2 3 diagonal and it turns a row vector into a column
vector.
4 5 6
7 8 9];
>> x'
ans =
1 4 7
2 5 8
3 6 9
>> v=[1 2 3];
>> v'
ans =
1
2
3
4 9 2
inv(x) produce the inverse of matrix x.
>> x=[1 4;
5 8];
>> inv(x)
ans =
Note :
a) replace the first five elements of vector x with its maximum value.
b) reshape this vector into a 3 x 3 matrix.
solution :
a) >> x(1:5)=max(x)
b) >> y(1,:)=x(1:3);
>> y(2,:)=x(4:6);
>> y(3,:)=x(7:9);
>> y
solution :
>> b=1:10
b=
1 2 3 4 5 6 7 8 9 10
>> b=b';
Task 1
1- Use edit command to edit the dct function, then try to edit sin function. State
the difference.
2- Use help command to get help about rand function.
3- Enter a=3; b=5; c=7, then clear the variable b only
Task 2
4- If x=[1 4; 8 3], find :
a) the inverse matrix of x .
b) the diagonal of x.
c) the sum of each column and the sum of whole matrix x.
d) the transpose of x.
a) replace the first row elements of matrix x with its average value.
b) reshape this matrix into row vector.
8- Generate the following row vector b=[5, 10, 15, 20 . . . . . . . . . 95, 100], then
find the number of elements in this vector.
Task 3:
Defining a matrix A = [ 1 8 3;5 2 0 ; 4 1 9]
1. extract the element in the 2nd row and 3rd column
2. picks the 3rd row of the matrix
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING