Matlab File - Deepak - Yadav - Bca - 4TH - Sem - A50504819015
Matlab File - Deepak - Yadav - Bca - 4TH - Sem - A50504819015
Matlab File - Deepak - Yadav - Bca - 4TH - Sem - A50504819015
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.
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.
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
atan2(x,
y) To compute the arctangent or inverse of y/x
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:
>> 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)
>> length(v)
Part 7: numel(x)
>> numel(v)
>> x=[1 2
45
7 8 ];
>> numel(x)
Part 8: single quote (‘)
>> x=[1 2 3
456
7 8 9];
>> x'
>> v'
Part 9:
>> x=[1 2 3
4 5 6];
>> max(max(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)
>> 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)
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 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
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)
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’)
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);
Code:
>>I=imread(‘noise.jfif’);
>>J=medfilt2(I);
>>imshow(J)