8 Basic Matrix Operations
8 Basic Matrix Operations
PROGRAM
Program 1
%Matrix addition
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
if(ma==mb&&na==nb)
Page 1 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations
c =a+b;
disp(' The sum of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for addition');
end
Program 2
%Matrix Subtraction
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
if(ma==mb&&na==nb)
c =a-b;
disp(' The Difference of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for subtraction');
end
Program 3
%matrix multiplication
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
[ma na] = size(a);
[mb nb] = size(b);
c =a.*b;
disp(' The Element wise product of two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for array multiplication');
end
elseif(w==2)
if(na==mb)
c =a*b;
disp(' The Matrix Product of two matrices is')
Page 2 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations
disp(c);
else
disp(' No.of columns of 1st matrix must be equal to No.of rows of 2nd matrix for
matrix multiplication');
end
else
disp(' You have not selected any operation');
end
Program 4
%Matrix Division
clc
clear all;
a = input('\nEnter the first matrix = ');
b = input('\nEnter the second matrix = ');
[ma na] = size(a);
[mb nb] = size(b);
if(ma==mb&&na==nb)
c =a./b;
disp(' The Element wise quotient two matrices is')
disp(c);
else
disp(' Matrix dimensions Must agree for array Division');
end
Program 5
%inverse of a matrix
a = input('\nEnter the matrix = ');
[ma na] = size(a);
if(na~=ma)
disp('The matrix is not square');
elseif(det(a)==0)
disp('The matrix is singular');
else
c=inv(a);
disp(' The Result is')
disp(c);
end
PROCEDURE
1. Open MATLAB software
2. Select Desktop Desktop layout Default; to get all relevant sub-windows on a single
window
Page 3 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Basic Matrix Operations
Page 4 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala