0% found this document useful (0 votes)
23 views6 pages

Lab On C 1

Uploaded by

akammiea Ahmed
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)
23 views6 pages

Lab On C 1

Uploaded by

akammiea Ahmed
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/ 6

Problem Statement:

Implement Matrix Operations


1. Addition
2. Subtraction
3. Multiplication

Analysis:
Matrix operation – Multiplication: Matrix multiplication, also known as matrix
product and the multiplication of two matrices, produces a single matrix. It is a type
of binary operation.
If A and B are the two matrices, then the product of the two matrices A and B are
denoted by:
X = AB
Hence, the product of two matrices is the dot product of the two matrices.
If A is a m×n matrix and B is a p×q matrix, then the matrix product of A and B is
represented by:
X = AB
Where X is the resulting matrix of m×q dimension.
Matrix multiplication Rules
a. The product of two matrices A and B is defined if the number of columns of A
is equal to the number of rows of B.
b. If AB is defined, then BA need not be defined
c. If both A and B are square matrices of the same order, then both AB and BA
are defined.
d. If AB and BA are both defined, it is not necessary that AB = BA.
e. If the product of two matrices is a zero matrix, it is not necessary that one of
the matrices is a zero matrix.

Matrix operation – Addition: Addition of matrix is the basic operation performed,


to add two or more matrices. Matrix addition is possible only if the order of the given
matrices are the same. By order we mean, the number of rows and columns are the
same for the matrices. Hence, we can add the corresponding elements of the matrices.
But if the order is different then matrix addition is not possible. Suppose A = [aij]mxn
and B = [bij]mxn are two matrices of order m x n, then the addition of A and B is given
by;
A + B = [aij]mxn + [bij]mxn = [aij + bij]mxn
By recalling the small concept of addition of algebraic expressions, we know that
while the addition of algebraic expressions can only be done with the corresponding
like terms, similarly the addition of two matrices can be done by addition of
corresponding terms in the matrix.
There are basically two criteria that define the addition of a matrix. They are as
follows:
a. Consider two matrices A & B. These matrices can be added if (if and only if)
the order of the matrices are equal, i.e. the two matrices have the same number
of rows and columns. For example, say matrix A is of the order 3 × 4, then the
matrix B can be added to matrix A if the order of B is also 3 × 4.
b. The addition of matrices is not defined for matrices of different sizes.

Matrix operation – Subtraction: Mathematically, if there are two matrices, say A =


[aij] and B = [bij] of the same order, say m × n, then the subtraction of A and B, i.e., A
– B is defined as:
Matrix D = [dij]
A – B = aij – bij
Thus, dij = aij – bij, (i = 1,2,3,… and j= 1,2,3…)

Algorithm:
Matrix operation – Multiplication:
Input: matrices A and B
Let C be a new matrix of the appropriate size
For i from 1 to n:
For j from 1 to p:
Let sum = 0
For k from 1 to m:
Set sum ← sum + Aik × Bkj
Set Cij ← sum
Return C

Matrix operation – Addition:


Input: matrices A and B
Let C be a new matrix of the appropriate size
For i from 1 to n:
For j from 1 to p:
Set Cij ← Aik + Bkj
Return C

Matrix operation – Subtraction:


Input: matrices A and B
Let C be a new matrix of the appropriate size
For i from 1 to n:
For j from 1 to p:
Set Cij ← Aik - Bkj
Return C

Source Code:
[P.T.O]
Input Output
Matrix Multiplication
Input Output
Matrix Addition
Input Output
Matrix Subtraction
Remarks
Here only Addition, Subtraction & Multiplication are implemented. Technically, there
is no such thing as matrix division. Dividing a matrix by another matrix is an
undefined function. The closest equivalent is multiplying by the inverse of another
matrix. In other words, while [A] ÷ [B] is undefined, you can solve the problem [A]
* [B]-1.

You might also like