[go: up one dir, main page]

0% found this document useful (0 votes)
65 views28 pages

Dip Lab Manual 1 Updated

The document is a lab manual for Digital Image Processing at UET Taxila, detailing the fundamentals of digital image processing, the use of MATLAB as a tool, and basic commands for manipulating images and data. It covers topics such as creating and referencing vectors and matrices in MATLAB, along with practical examples and commands. The manual serves as a guide for students in the Software Engineering Department to enhance their understanding of digital image processing techniques using MATLAB.

Uploaded by

Basat Maqsood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views28 pages

Dip Lab Manual 1 Updated

The document is a lab manual for Digital Image Processing at UET Taxila, detailing the fundamentals of digital image processing, the use of MATLAB as a tool, and basic commands for manipulating images and data. It covers topics such as creating and referencing vectors and matrices in MATLAB, along with practical examples and commands. The manual serves as a guide for students in the Software Engineering Department to enhance their understanding of digital image processing techniques using MATLAB.

Uploaded by

Basat Maqsood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

DIGITAL IMAGE PROCESSING

LAB MANUAL 1

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Digital Image Processing

 In digital image processing, an image is a two-dimensional matrix of pixels, or picture


elements, arranged in rows and columns.
 Each pixel has a specific value that represents its intensity or gray level.
 Digital image processing is the use of algorithms and mathematical models to process and
analyze digital images.
 The goal of digital image processing is to enhance the quality of images, extract meaningful
information from images, and automate image-based tasks.
 Digital image processing is widely used in a variety of applications, including medical
imaging, remote sensing, computer vision, and multimedia.

MATLAB : A Tool for DIP


 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.
 The name MATLAB stands for matrix laboratory.
 MATLAB was originally written to provide easy access to matrix.

Starting and Quitting MATLAB


 To start MATLAB, double-click the MATLAB shortcut icon on your Windows desktop.
 You will know MALTAB is running when you see the special " >> " prompt in the
MATLAB Command Window.
 To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or
type quit (or exit) in the Command Window, or with easy way by click on close button
in control box.

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Desktop Tools
1- Command Window: Use the Command Window to enter variables and run
functions and M-files.
2- Command History: Statements you enter in the Command Window are logged
in the Command History. In the Command History, you can view previously run
statements, and copy and execute selected statements.
3- Current Directory Browser: MATLAB file operations use the current directory
reference point. Any file you want to run must be in the current directory or on
the search path.
4- Workspace: The MATLAB workspace consists of the set of variables (named
arrays) built up during a MATLAB session and stored in memory.

Current Directory Browser Command Window

Workspace

Command
History

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

5- Editor/Debugger Window: Use the Editor/Debugger to create and debug M-


files.

Basic Commands

• clear Command: Removes all variables from workspace.

• 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.

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Notes:
• A semicolon " ; " at the end of a MATLAB statement suppresses printing of results.
>> S=10;

>>S=10

Output:

S=10

• 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
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

>> clear a
>> a % can you see the effect of clear command
>> clc % clean the screen

Introduction to Vectors in Matlab

A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors −

 Row vectors
 Column vectors

Row Vectors

 Row vectors are created by enclosing the set of elements in square brackets.
 It uses space or comma to delimit the elements.

r = [7 8 9 10 11]

Output:

r =

7 8 9 10 11

Column Vectors

 Column vectors are created by enclosing the set of elements in square brackets, using
semicolon to delimit the elements.

c = [7; 8; 9; 10; 11]

Output:
c =
7

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

8
9
10
11

Defining a Vector

 Matlab is a software package that makes it easier for you to enter matrices and vectors, and
manipulate them.
 The interface follows a language that is designed to look a lot like the notation used in linear
algebra.
 Almost all of Matlab's basic commands revolve around the use of vectors.
 A vector is defined by placing a sequence of numbers within square braces:

>> v = [3 1 7 -21 5 6 4]

v=

3 1 7 -21 5 6 4

 This creates a row vector which has the label "v".


 The first entry in the vector is a 3 and the second entry is a 1.
 Note that Matlab printed out a copy of the vector after you hit the enter key.
 If you do not want to print out the result put a semi-colon at the end of the line:

>> v = [3 1 7 -21 5 6 4];

If you want to view the vector just type its label:

>>v

Output:
v=

3 1 7 -21 5 6 4
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.
 The transpose is defined using an apostrophe ("'"):

>> v = [3 1 7 -21 5 6 4]'

Output:

v=

3
1
7
-21
5
6
4

Or you can use the transpose operator ( .’ )

>> w=v.’

Output:
w=
3
1
7
-21
5
6
4

Referencing the Elements of a Vector

 You can reference one or more of the elements of a vector in several ways.
 The ith component of a vector v is referred as v(i).

For example −
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

>>v = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements


>>v(3)
Output:
ans = 3

 When you reference a vector with a colon, such as v(:),


 all the components of the vector are listed.

>>v = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements


>>v(:)
Output:
ans =
1
2
3
4
5
6

 MATLAB allows you to select a range of elements from a vector.


 For example, let us create a row vector rv of 9 elements
 then we will reference the elements 3 to 7 by writing rv(3:7)
 and create a new vector named sub_rv.

Live Demo
rv = [1 2 3 4 5 6 7 8 9];
sub_rv = rv(3:7)
Output:
3 4 5 6 7

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 Similarly to access all elements from some specific location say the 3rd through the last
element

>>v=[1 2 3 4 5 6 7 8 9];
>>v(3:end)

Output:

ans =

3 4 5 6 7 8 9

 Produces a column vector

>>v=[1 2 3 4 5 6 7 8 9];
>>v(:)

Output:
ans =

1
2
3
4
5
6
7
8
9

 Produces a row vector

>>v=[1 2 3 4 5 6 7 8 9];
>>v(1:end)

Output:

ans =

1 2 3 4 5 6 7 8 9
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 Indexing is not restricted to contiguous locations.


 Combine the colon operator and end to achieve a variety of effects, such as extracting
every k-th element or flipping the entire vector.

>>v=[1 2 3 4 5 6 7 8 9];
>>v(1:2:end) % Extract all the odd elements
ans =
1 3 5 7 9

>>v=[1 2 3 4 5 6 7 8 9];
>>v(end:-1:1) % Reverse or Flip the order of elements
ans =
9 8 7 6 5 4 3 2 1

>>v=[1 2 3 4 5 6 7 8 9];
>>v(end:-2:1) % show odd elements only after Reverse or Flip
ans=9 7 5 3 1

 You can even do arithmetic using end.

>>v=[1 2 3 4 5 6 7 8 9];
v(2:end-1) % Extract the second through the next-to-last elements
ans =
5 9 4 2 11 7

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Creating Large Vectors

 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

2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000

Columns 8 through 9

3.7500 4.0000

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 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

Introduction to Matrices in Matlab

 A matrix is a two-dimensional array of numbers.

 In MATLAB, you create a matrix by entering elements in each row as comma or space

delimited numbers and using semicolons to mark the end of each row.

For example, let us create a 4-by-5 matrix

a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
a =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

 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 =
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

1 2 3
3 4 5
6 7 8

 You can also treat it like a row of column vectors:

>> B = [ [1 2 3]' [2 4 7]' [3 5 8]']


B =

1 2 3
2 4 5
3 7 8

Referencing to individual element of a Matrix

 To reference an element in the mth row and nth column, of a matrix mx, we write −

mx(m, n);

 For example, to refer to the element in the 2nd row and 5th column, of the matrix a, as
created in the last section, we type

Live Demo
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)
ans = 6

 To reference all the elements in the mth column we type A(:,m).

 Let us create a column vector v, from the elements of the 4th row of the matrix a −

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Live Demo
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)
v =
4
5
6
7

 You can also select the elements in the mth through nth columns, for this we write −

a(:,m:n)

 To extract the element in the 2nd row and 3rd column:

>> 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:

 Let us create a smaller matrix taking the elements from the second and third
columns −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)
2 3
3 4
4 5
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

5 6

 create a sub-matrix sa taking the inner subpart of a


Live Demo
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
sa = a(2:3,2:4)
sa =
3 4 5
4 5 6

 Array Index out of bound


>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(1:2,3:4)
??? Index exceeds matrix dimensions.

 Slicing matrix A

 Transposing the sub matrix

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(1:2,2:3)'
ans =
2 4
3 5

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>C= A( : , 3) % this statement picks the third column of the matrix
ans=
C=

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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

 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
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

7 8

 Matrix addressing can also be used in the following way


>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>E= logical ([1 0 0; 0 0 1; 0 0 0])
E=
1 0 0
0 0 1
0 0 0
 Now if we write
>>A(E)
ans =
1
5

 The use of a colon is useful in case to find the sum of all elements of a matrix

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>s= sum (A(:))
s =39

 Some important standard arrays are:

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

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 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.

>> magic (3)


ans =
8 1 6
3 5 7
4 9 2

Basic Matrix Functions


Command Description

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

sum(x)  The sum of the elements of x.


>> x=[ 1 2 3; 3 4 5; 6 7 8];  For matrices, sum(x) is a row vector with the
>> sum(x) sum over each column.
ans =  sum (x,dim) sums along the dimension dim.
10 13 16
>>sum(sum(x))
ans = 39
>> sum(x,2) ans= 6
12 21

size(x)  return the size (dimensions) of matrix x.

>>x=[ 1 2 3; 3 4 5; 6 7 8]
>> size(x)
ans = 3 3

return the length (number of elements) of vector


length(v) 
v.
>> v=[1 2 3];
>> length(v)
ans = 3

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

numel(x)  returns the number of elements in array x.


>> v =[55 63 34];
>> numel(v)
ans = 3
>> x=[1 2
4 5
4 8 ];
>> numel(x)
ans = 6

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

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

max(x)  Find the largest element in a matrix or a vector.


>> x=[1 2 3
4 5 6];
>> max(x)
>> ans= 4 5 6
>> max(max(x))
ans = 6

min (x)  Find the smallest element in a matrix or a


vector.
>> x=[1 2 3
5 5 6];
>>min(x)
ans = 1 2 3
>> min(min(x))
ans = 1

magic(N)  produce N Magic square.


 This command produces valid magic squares for
>> magic(3) all N>0 except N=2.
ans =
8 1 6
3 5 7

4 9 2
inv(x)  produce the inverse of matrix x.
>> x=[1 4;
5 8];
>> inv(x)

ans =

-0.6667 0.3333 0.4167


-0.0833

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

diag(x)  Return the diagonal of matrix x.


 if x is a vector then this command produce a
>> x=[ 1 2 3 diagonal matrix with diagonal x.
4 5 6
7 8 9];
>> diag(x)
ans =
1
5
9
>> v=[1 2 3];
>> diag(v)
ans =
1 0 0
0 2 0
0 0 3

median(x)  The median value of the elements of x.


x=[ 4 6 8  For matrices, median (x) is a row vector
10 9 1 with the median value for each column.
4 2 5];
>> median(x)
ans =
7 6 5
>> median(x,2)  median(x,dim) takes the median along the
dimension dim of x.
ans =
6
9
5
>> median(median(x))
ans = 6
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

sort(x,DIM,MODE) Sort in ascending or descending order.


>> x = [3 7 5  - For vectors, sort(x) sorts the elements of x
0 4 2]; in ascending order.
>> sort(x,1)
ans =  For matrices, sort(x) sorts each column of
0 4 2 x in ascending order.
3 7 5
>> sort(x,2)
ans = DIM= 1 by default
3 5 7
0 2 4 MODE= 'ascend' by default
>> sort(x,2,'descend')
ans =
7 5 3
4 2 0

tril(x)  Extract lower triangular part of matrix x.


>> x=[5 1 8
4 7 3
2 5 6];
>> tril(x)
ans =
5 0 0
4 7 0
2 5 6

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

triu(x)  Extract upper triangular part of matrix x.


>> x=[5 1 8
4 7 3
2 5 6];
>> triu(x)
ans =
5 1 8
0 7 3  Create a matrix is to use a function, such
0 0 6 as ones, zeros, or rand. For example,
create a 5-by-1 column vector of zeros.
>>z = zeros(5,1)
z =
0
0
0
0
0

Note :

 Arithmetic operations on arrays are done element-by-element.


 This means that addition and subtraction are the same for arrays and matrices, but that
multiplicative operations are different.
 MATLAB uses a dot ( . ) or decimal point, as part of the notation for multiplicative array
operations.
Example1: Find the factorial of 5
Solution :
>>x=2:5
>>prod(x)
x=
2 3 4 5

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example2: if x = [1,5,7,9,13,20,6,7,8], then

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

Example3: Generate the following row vector b=[1, 2, 3, 4, 5, . . . . . . . . . 9,10],


then transpose it to column vector.

solution :

>> b=1:10
b=
1 2 3 4 5 6 7 8 9 10
>> b=b';

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

5- If x= [2 8 5; 9 7 1], b=[2 4 5] find:


a) find the maximum and minimum of x.
b) find median value over each row of x.
c) add the vector b as a third row to x.

6- If x=[ 2 6 12; 15 6 3; 10 11 1], then

a) replace the first row elements of matrix x with its average value.
b) reshape this matrix into row vector.

7- Generate a 4 x 4 Identity matrix.

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

SOFTWARE ENGINEERING DEPARTMENT

3. picks the 2nd column of the matrix


4. create a matrix B equal to A but with its last column set to 0’s
5. find the sum of all elements of a matrix

Digital Image Processing 6th Term-SE UET Taxila

You might also like