0% found this document useful (0 votes)
7 views

8 Basic Matrix Operations

The document outlines basic matrix operations in MATLAB, including addition, subtraction, multiplication, division, and finding the inverse of a matrix. It provides detailed MATLAB programs for each operation, along with conditions for valid operations based on matrix dimensions. Additionally, it includes a step-by-step procedure for running the programs in MATLAB.

Uploaded by

Divyashree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

8 Basic Matrix Operations

The document outlines basic matrix operations in MATLAB, including addition, subtraction, multiplication, division, and finding the inverse of a matrix. It provides detailed MATLAB programs for each operation, along with conditions for valid operations based on matrix dimensions. Additionally, it includes a step-by-step procedure for running the programs in MATLAB.

Uploaded by

Divyashree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

BASIC MATRIX OPERATIONS

AIM:- To write matlab programs for basic matrix operation.


OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To identify basic mathematical operators in MATLAB for matrix
manipulation
Operators
Arithmetic operations
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
To write a matrix product as C=A*B where A is an m x n matrix and B is an n x k
matrix. MATLAB allows to be carried out on matrices in straightforward ways as long as the
operation makes sense mathematically and the operands are compatible. Thus,
A+B or A-B is valid if A and B are of the same size,
A*B is valid if A's number of columns equals B's number of rows,
A/B is valid and equals A · B -1 for same-size square matrices, and
A^2 which equals A*A, makes sense only if A is square.
Array operation:
·* element-by-element multiplication
./ element-by-element left division
.\ element-by-element right. Division
.^ elernerrt-by-element exponentiation
.’ nonconjugated transpose
Examples:
If U = [u1 u2 u3…….] and V = [v1 v2 v3 …………….] then
U . *V produces [u1v1 u2v2 u3v3 . . . ]
U . /V produces [ u1/v1 u2/v2 u3/ v3 ... ]
U . ^V produces [ u1v1 u2v2 u3v3 …..]

PROGRAM
Program 1
%Matrix addition
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)

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 = ');

[ma na] = size(a);


[mb nb] = size(b);

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);

w= input('Enter \n 1 for Array Multiplication\n 2 for Matrix multiplication\n =');


if(w==1)
if(ma==mb&&na==nb)

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

3. Now type the following on command window


clc
A = [1 2 3 ; 4 5 -1 ; 7 4 2]
B = [1 4 1 ; 3 2 9; 3 -5 1]
C=A+B
D=A-B
S = inv(A)
T = inv(B)
E = A .* B
F=A*B
N = A ./B
J = A/B
K = A*(inv(B))
L=A\B
M = inv(A) * B
4. Now create a script file( .m file) by selecting File  New Blank M-file
5. Type the program1 and save it as basic_op.m
(Note: File Name should be different from any variable names, in built functions, and do
not use any special character other than underscore . The first character of the name of a
file or variable should be an alphabet)
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on the debug
toolbar
7. On command window Enter the input matrices and press Enter
8. Observe and note down the Result
9. Repeat Steps 4 to 8 for other programs

Page 4 of 4
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala

You might also like