Matlab File - Deepak - Yadav - Bca - 4TH - Sem - A50504819015

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 59

IMPLEMENTATION PRACTICAL ON MATLAB

LAB FILE

SUBMITTED TO
Mr. Manoj Kumar Pandey

SUBMITTED BY
Deepak Yadav
BCA | A5054819015
INDEX
S.no Name of Experiments Date Remarks
1 Introduction to MATLAB 25/01/2021
2 To Implement basic MATLAB commands 25/01/2021
3 To Implement and work with Matrices in 01/02/2021
MATLAB
4 To Implement operators and expressions in 02/02/2021
MATLAB
5 To Implement relational and logical 08/02/2021
Operations in MATLAB
6 To Implement plotting function in MATLAB 09/02/2021
7 To Implement strings in MATLAB 16/02/2021
8 To Implement flow control in MATLAB 22/02/2021
9 To Implement basic image processing 01/03/2021
commands in MATLAB
10 To Implement histogram equalization in 02/03/2021
MATLAB
11 To Implement gamma equalization in 09/03/2021
MATLAB
12 To Implement average filtering in MATLAB 16/03/2021
13 To Implement noise reduction in MATLAB 22/03/2021
Experiment No. 1

Introduction to MATLAB

MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical
computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984.It is
written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of
algorithms and creation of user interfaces.

Getting started with MATLAB:


It is both a programming language as well as a programming environment. It allows the computation of
statements in the command window itself.

 Command Window:
In this window one must type and immediately execute the statements, as it requires quick
prototyping. These statements cannot be saved. Thus, this is can be used for small, easily
executable programs.

 Editor (Script):
In this window one can execute larger programs with multiple statements, and complex
functions These can be saved and are done with the file extension ‘.m ‘

 Workspace:
In this window the values of the variables that are created in the course of the program (in the
editor) are displayed.

 <="" b="" style="box-sizing: border-box;">


This window displays the exact location(path) of the program file being created.

MATLAB Library comes with a set of many inbuilt functions. These functions mostly perform
mathematical operations like sine, cosine and tangent. They perform more complex functions too like
finding the inverse and determinant of a matrix, cross product and dot product

Although MATLAB is encoded in C, C++ and Java, it is a lot easier to implement than these three
languages. For example, unlike the other three, no header files need to be initialized in the beginning of
the document and for declaring a variable, the data type need not be provided. It provides an easier
alternative for vector operations. They can be performed using one command instead of multiple
statements in a for or while loop.

Some of the basic functions in MATLAB and their uses are listed below:
Function Description

The values or the text printed within single


disp() quotes is displayed on the output screen

clear To clear all variables

close all To close all graphics window

clc To clear the command window

To compute the exponential value of x to the


exp(x) base e

abs(x) To compute the absolute value of x

sqrt(x) To compute the square root of x

To compute the logarithmic value of x to the


log(x) base e

To compute the logarithmic value of x to the


log10(x) base 10

rem(x, y) To compute the remainder of x/y

sin(x) To compute the sine of x


Function Description

cos(x) To compute the cosine of x

tan(x) To compute the tangent of x

atan2(x,
y) To compute the arctangent or inverse of y/x

Writing a MATLAB program:

1. Using Command Window:


Only one statement can be typed and executed at a time. It executes the statement when the
enter key is pressed. This is mostly used for simple calculations.

Note: ans is a default variable created by MATLAB that stores the output of the given computation.

2. Using Editor:
Multiple lines of code can be written here and only after pressing the run button (or F5) will the
code be executed. It is always a good practice to write clc, clear and close all in the beginning of
the program.

Note: Statements ending with a semicolon will not be displayed in the command window, however,
their values will be displayed in the workspace.
Any statement followed by % in MATLAB is considered as a comment

3. Vector Operations:
Operations such as addition, subtraction, multiplication and division can be done using a single
command instead of multiple loops
Experiment No. 2
To Implement basic MATLAB commands
MATLAB is an interactive program for numerical computation and data
visualization. You can enter a command by typing it at the MATLAB prompt '>>'
on the Command Window.
CODE
>> a=3
>> a=3;
>> a+5
>> b=a+5
>> clear a
>> a
>>clc
>> b
OUTPUT – 1. For Addition
2. clc Command
Experiment No. 3
To Implement and work with 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.

CODE

Part 1:

>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

>> A(1,4) + A(2,4) + A(3,4) + A(4,4)

>> t = A(4,5)

>> X = A;

>> X(4,5) = 17
Part 2:

>> 1:10

>> 100:-7:50

Part 3:

>> A(4,:)

>> A(4,1:end)

>> A(2,end)
Part 4: sum(x)

>> x=[1 2 3

4 5 6];

>> sum(x)

>> sum(x,2)

>>sum(sum(x))
Part 5: mean(x)

x=[1 2 3; 4 5 6];

>> mean(x)

>> mean(x,2)

>>mean(mean(x))
Part 6:

>> zeros(2,3)

>> ones(2,3)

>> x=[1 2 3

4 5 6];

>> size(x)

>> v=[1 2 3];

>> length(v)
Part 7: numel(x)

>> v =[55 63 34];

>> numel(v)

>> x=[1 2

45

7 8 ];

>> numel(x)
Part 8: single quote (‘)

>> x=[1 2 3

456

7 8 9];

>> x'

>> v=[1 2 3];

>> v'
Part 9:

>> x=[1 2 3

4 5 6];

>> max (x)

>> max(max(x))

>> min (x)

>> min(min(x))

>> magic(3)
Part 10:

>> x=[1 4;

5 8];

>> inv(x)

>> x=[1 2 3

456

7 8 9];

>> diag(x)

>> v=[1 2 3];

>> diag(v)
Part 11:

>> x=[1 2 3

4 5 6];

>> prod(x)

>> prod(prod(x))

>>x=[4 6 8

10 9 1

8 2 5];

>> median(x)

>> median(x,2)

>> median(median(x))
Part 12:

>> x = [3 7 5

0 4 2];

>> sort(x,1)

>> sort(x,2)

>> sort(x,2,'descend')

>> x=[5 1 8

473

2 5 6];

>> det(x)

>> x=[5 1 8

473
2 5 6];

>> tril(x)

>> x=[5 1 8

473

2 5 6];

>> triu(x)
Experiment No. 4
To Implement operators and expressions in MATLAB
Code:
Part 1:
>> x = (1+sqrt(5))/2
>> a = abs(3+4i)
>> y=sin(pi/3)+cos(pi/4)-2*sqrt(3)

Part 2: System: [x+y=1; x-y+z=0; x+y+z=2]


>> a=[1 1 0; 1 -1 1; 1 1 1]; b=[1;0;2];
>> x=inv(a)*b
>> x=a\b
Part 3: round, fix, ceil, floor commands
>>round(1.3)
>>fix(1.5)
>>round(1.9)
>>fix(11.9)
>>ceil(-2.9)
>>floor(-3.9)
>>round(3.4)
Experiment No. 5
To Implement relational and logical Operations in
MATLAB

Code:
Part 1: Logical Operation
>> a=1:9; b=9-a;
>> t=a>4
>> t= (a==b)
>> a = [0 4 0 -3 -5 2];
>> b = ~a
>> c=a&b
>> x>2
>> t=~(~x);
>> sum(sum(t))

Part 2: Bitwise Operation


>> A=5; B=6;
>> bitget(A,3)
>> bitget(A,(1:8))
>> bitand(A,B)
>> and(A,B)
Part 3: Logical Operation
>>A=[4 9 7 0 5];
>> any(A)
>> all(A)
>> find(A)
>> B=A(find(A));
>> B
>> find(B==max(B))

Part 4:[ v = (x and y) or z; w = not (x or y) and z; u = (x and not (y)) or (not (x) and
y)]
>>x=1;y=0;z=1;
>>v=bitor(bitand(x,y),z)
>>w=bitand((~bitor(x,y)),z)
>>u=bitor(bitand(x,~y),bitand(~x,y))
Experiment No. 6
To Implement plotting function in MATLAB
Code:
Part 1:
>>x = 0:pi/100:2*pi;
>>y = sin(x);
>>plot(y)
>>plot(x,y)
Part 2:
>>x1 = 0:pi/100:2*pi;
>>x2 = 0:pi/10:2*pi;
>>plot(x1,sin(x1),'b:',x2,cos(x2),'y+')
Part 3:
>>x1 = 0:pi/100:2*pi;
>>x2 = 0:pi/10:2*pi;
>>plot(x1,sin(x1),'b:')
>>plot(x2,cos(x2),'r+')
Part 4: Multiple plots in one figure
t = 0:pi/10:2*pi;
x=sin(t); y=cos(t); z= 2*y-3*x; v=5-z;
subplot(2,2,1); plot(x)
subplot(2,2,2); plot(y)
subplot(2,2,3); plot(z)
subplot(2,2,4); plot(v)
Part 5:Axis and labels
t = -pi:pi/100:pi;
y = sin(t);
plot(t,y)
axis([-pi pi -1 1])
Experiment No. 7
To Implement strings in MATLAB
Code:
Part 1:
>> S = 'Hello'
>> h = ['MAT', 'LAB']
>> v = ['MAT'; 'LAB']

Part 2:
>> char(100)
>> char([73 82 65 81])
>> double('z')
>> double('ali')
>>strcat('Hello',' Ali')
>> strvcat ('Hello', 'Hi', 'Bye')
>> num2str(20)
>> str2num('20')
>>error (Msg)
Part 3: To enter matrix or vector or single element
>> x=input('parameter= ')
parameter= 2
>> x=input('parameter= ')
parameter= [2 4 6]
>> x=input('parameter= ')
parameter= [1 2 3;4 5 6]
Part 4: To enter text and using disp (x)
>> x=input('parameter= ')
parameter= 'faaz'
>> x=input('parameter= ' , 's' )
parameter= faaz
>> x=[1 2 3];
>> disp(x)

Part 5:
>> a=6;
>> b=a;
>> s='Ahmed has ';
>> w='Ali has ';
>> t=' Dinars';
>>disp([ s num2str(a) t]);
>>disp([ w num2str(b) t]);

Experiment No. 8
To Implement flow control in MATLAB
Code:
Part 1: if statement
A=input('A=');
B=input('B=');
if A > B
'greater'
else
if A < B
'less'
elseif A == B
'equal'
else
error ('Unexpected situation')
end
Part 2: switch and case statement
method = 'Bilinear';
switch lower(method)
case 'bilinear'
disp('Method is bilinear')
case 'cubic'
disp('Method is cubic')
case 'nearest'
disp('Method is nearest')
otherwise
disp('Unknown method.')
end

Part 3: for statement


for i=1:5
for k=5:-1:1
m(i,k)=i*k;
end
end
Part 4: for statement for sin function
for i=1:10
x(i)=sin (pi/i);
i=10;
end
x
i
Part 5: while statement
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
Part 6: continue function using break statement
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if fx == 0
break
elseif sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
Part 7: maximum of a matrix without using max command
a=[11 3 14;8 6 2;10 13 1]
temp=a(1);
[n,m]=size(a);
for i=1:n
for j=1:m
if a(i,j)>temp
temp=a(i,j);
end
end
end
temp
Part 8: Let x=[2 6; 1 8], y=[.8 -0.3 ; -0.1 0.2], prove that y is not the inverse matrix
of x.
z=inv(x);
if ~isequal(z,y)
disp(' y is not the inverse matrix of x ') end

d
Experiment No. 9
To Implement basic image processing commands in
MATLAB

Code:
Part 1: reading and displaying the image
>>a=imread(‘test1.PNG’)
>>imshow(a)

Part 2: rgb image to gray image conversion


>>b=rbg2gray(a);
>>imshow(b)

Part 3: gray image to black and white image conversion


>>c=im2bw(b)
>>imshow(c)

Part 4: displaying the image of a matrix


>>d=[12 34 56 67 78;45 43 23 23 65;67 34 0 0 0;0 0 0 0 0 ;0 0 0 0 0]
>>imshow(d)

Part 5: making a small part inside an image white


for i=50:100
for j=300:350
b(i,j)=255;
end
end
imshow(b)

Part 6: cropping an image


I=imread(‘test1.PNG’);
I2=imcrop(I,[75 68 130 112]);
subplot(1,2,1)
imshow(I)
title(‘Original Image’)
subplot(1,2,2)
imshow(I2)
title(‘Cropped Image’)

Part 7: resizing of image


>>J=imresize(I,0.2);
>>figure, imshow(I),figure , imshow(J)
Experiment No. 10
To Implement histogram equalisation in MATLAB

Code:
Part 1: Appling Histogram Equalization
>>a=imread(‘ddd.PNG’);
>>a=rgb2gray(a);
>>b=255-a;
>>imshow(b)
>>imhist(a)
>>b=histeq(a);
>>imshow(b)
>>imhist(b)
Part 2: Contrast Stretching vs Histogram Equalization
a=imread(‘img.jpg’);
b=imadjust(a);
c=histeq(a);
subplot(1,2,1)
imshow(b)
title(‘Contrast Stretching’)
subplot(1,2,2)
imshow(c)
title(‘Histogram Equalization’)
Experiment No. (11)
To Implement gamma equalization in MATLAB

Code:
Part 1: Method One of gamma equalization using imadjust()
a=imread(‘river.png’);
I=rbg2gray(a);
size(I) J=imadjust(I,
[],[],3);
subplot(1,2,1)
imshow(I)
title(‘Original Image’)
subplot(1,2,2)
imshow(J)
title(‘Gamma Equalization’)

Part 2: Method two of gamma equalization using gamma function


r=double(I)/255;
c=1;
gamma=3;
s=c*(r).^gamma;
subplot(1,2,1)
imshow(I)
title(‘Original Image’)
subplot(1,2,2)
imshow(s)
title(‘Gamma Equalization’)
Experiment No. 12
To Implement average filtering in MATLAB

Code:
Part 1: For average filtering of 3x3
grayImage=imread(‘123.png’);
grayImage=rbg2gray(grayImage);
subplot(2,1,1);
imshow(grayImage);
title(‘Original Image’,’FontSize’,15);
blurredImage=imfilter(grayImage,ones(3)/9,’symmetric’);
subplot(2,1,2);
imshow(blurredImage);
title(‘Blurred Image’,’FontSize’,15);

Part 2: For average filtering of 5x5


grayImage=imread(‘123.png’);
grayImage=rbg2gray(grayImage);
subplot(2,1,1);
imshow(grayImage);
title(‘Original Image’,’FontSize’,15);
blurredImage=imfilter(grayImage,ones(5)/25,’symmetric’);
subplot(2,1,2);
imshow(blurredImage);
title(‘Blurred Image’,’FontSize’,15);

Part 3: For average filtering of 9x9


grayImage=imread(‘123.png’);
grayImage=rbg2gray(grayImage);
subplot(2,1,1);
imshow(grayImage);
title(‘Original Image’,’FontSize’,15);
blurredImage=imfilter(grayImage,ones(9)/81,’symmetric’);
subplot(2,1,2);
imshow(blurredImage);
title(‘Blurred Image’,’FontSize’,15);

Part 4: For average filtering of 15x15


grayImage=imread(‘123.png’);
grayImage=rbg2gray(grayImage);
subplot(2,1,1);
imshow(grayImage);
title(‘Original Image’,’FontSize’,15);
blurredImage=imfilter(grayImage,ones(15)/225,’symmetric’);
subplot(2,1,2);
imshow(blurredImage);
title(‘Blurred Image’,’FontSize’,15);
Experiment No. 13
To Implement noise reduction in MATLAB

Code:
>>I=imread(‘noise.jfif’);
>>J=medfilt2(I);
>>imshow(J)

You might also like