0% found this document useful (0 votes)
22 views25 pages

Matrix Algebra

Uploaded by

hasnainwaris14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

Matrix Algebra

Uploaded by

hasnainwaris14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Department of BES-1

COMPUTATIONAL THINKING FOR


STRUCTURED DESIGN

Topic:

MATRIX ALGEBRA
Session - 31

CREATED BY K. VICTOR BABU


AIM OF THE SESSION

To familiarize students with the basic concept of Necessity of Matrix Algebra Functions using Arrays

INSTRUCTIONAL OBJECTIVES
This Session is designed to:
• Utilize a 2-D Array to implement the addition of two 2-D Matrices.
• Implement the multiplication of two 2-D Matrices.
• Calculate the trace of a 2-D Matrix.
• Gain a comprehension of Matrix algebra using functions.
• Implement the Transpose of a 2-D Matrix.

LEARNING OUTCOMES
At the end of this session, you should be able to:
Successfully implement the addition of two 2-D Matrices using a 2-D Array.
Effectively perform the multiplication of two 2-D Matrices.
Calculate the trace of a given 2-D Matrix accurately.
Demonstrate a clear understanding of Matrix algebra, incorporating functions.
Implement the Transpose operation on a 2-D Matrix, producing the desired result.

CREATED BY K. VICTOR BABU


INTRODUCTION

This Session will discuss about …


• By using 2-D array we will implement the different Matrix Algebra like
Matrix Multiplication, Transpose of Matrix and check Identity Matrix etc.

Multiplicati
on

Matrix Transpose
Identity
Matrix
CREATED BY K. VICTOR BABU
INTRODUCTION

Matrices
 Matrix is a rectangular array of numbers, symbols, points, or
characters each belonging to a specific row and column. A matrix is
identified by its order which is given in the form of rows ⨯ and
columns.
 The numbers, symbols, points, or characters present inside a matrix
are called the elements of a matrix.
 The location of each element is given by the row and column it
belongs to.

CREATED BY K. VICTOR BABU


INTRODUCTION

What are Matrices?


Matrices are rectangular arrays of numbers, symbols, or characters
where all of these elements are arranged in each row and column.
An array is a collection of items arranged at different locations.This
array of points is called a matrix.
Matrices Definition
A rectangular array of numbers, symbols, or characters is called a
Matrix. Matrices are identified by their order. The order of the
matrices is given in the form of a number of rows ⨯ number of
columns. A matrix is represented as [P] m⨯n where P is the matrix,
m is the number of rows and n is the number of columns.

CREATED BY K. VICTOR BABU


INTRODUCTION

• Order of Matrix
Order of the Matrix tells about the number of rows and columns
present in a matrix. Order of a matrix is represented as the number of
rows times the number of columns. Let’s say if a matrix has 4 rows
and 5 columns then the order of the matrix will be 4⨯5.
Examples of matrices are mentioned below:

CREATED BY K. VICTOR BABU


Operations on Matrices

• Matrices undergo various mathematical operations such as addition,


subtraction, scalar multiplication, and multiplication.
• These operations are performed between the elements of two
matrices to give an equivalent matrix that contains the elements
which are obtained as a result of the operation between elements of
two matrices.

CREATED BY K. VICTOR BABU


Addition of Two Matrices

• The elements of two matrices are added to yield a matrix that


contains elements obtained as the sum of two matrices.
• The addition of matrices is performed two matrices of the same
order.

CREATED BY K. VICTOR BABU


CREATED BY K. VICTOR BABU
Subtraction of Matrices

• Subtraction of Matrices is the difference between the elements of


two matrices of the same order to give an equivalent matrix of the
same order whose elements are equal to the difference of elements
of two matrices.

CREATED BY K. VICTOR BABU


CREATED BY K. VICTOR BABU
Session Description

Matrix Multiplication:
1.Start.
2.Enter the value of m and n (or) order of the first matrix.
3.Enter the value of p and q (or) order of the second matrix.
4.Create a matrix of size a[m][n] and b[p][q].
5.Enter the element of matrices row-wise using loops.
6.If the number of columns of the first matrix is not equal to the number of rows of the second
matrix, print matrix multiplication is not possible and exit. If not, proceed to the next step.
7.Create a third matrix, c of size m x q, to store the product.
8.Set a loop from i=0 to i=m.
9.Set an inner loop for the above loop from j=0 to j=q.
10.Initialise the value of the element (i, j) of the new matrix to 0.
11.Set an inner loop inside the above loop from k=0 to k=p.
12.Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third matrix, c[i][j].
13.Print the third matrix.
14.Stop.

CREATED BY K. VICTOR BABU


Session Description

Mathematical Explanation:

CREATED BY K. VICTOR BABU


Session Description

Basic Code for Matrix Multiplication:

for (i = 0; i< row; i++) {


for (j= 0; j < column; j++) {
for (k = 0; k < row; k++) {
sum = sum + mat1[i][k]*mat2[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
CREATED BY K. VICTOR BABU
Session Description

Algorithm for Transpose of a Matrix:

1.Declare and initialize a 2-D array p[a][b] of order axb.


2.Read the matrix p[a][b] from the user.
3.Declare another 2-dimensional array t to store the transpose of the matrix. This array
will have the reversed dimensions as of the original matrix.
4.The next step is to loop through the original array and convert its rows to the columns
of matrix t.
1. Declare 2 variables i and j.
2. Set both i,j=0
3. Repeat until i<b
1.Repeat until j<a
2.t[i][j] = p[j][i]
3.j=j+1**
4. i=i+1
5.The last step is to display the elements of the transposed matrix t.
CREATED BY K. VICTOR BABU
Session Description

Mathematical Explanation:

CREATED BY K. VICTOR BABU


Session Description

Basic Code for Matrix Multiplication:

for (i = 0; i< row; i++) {


for (j= 0; j < column; j++) {
t[i][j]=p[j][i];
}
}

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

Task 1:
Matrix Multiplication of two Matrices using two 2-D Array.

Task 2:
create a C program that utilizes a 2-D array to take runtime input for a
source matrix mat[3][3], and completes the following tasks?

(i) Computes the transpose of matrix mat[3][3] and stores the output in a
new matrix named transpose[3][3].
(ii) Verifies whether the diagonal elements of mat[3][3] and transpose[3]
[3] are in the same positions.
CREATED BY K. VICTOR BABU
EXAMPLES

Example of Matrix Multiplication:

CREATED BY K. VICTOR BABU


SUMMARY

End of the session student will be able to


implement matrix multiplication and Transpose of
Matrix using array.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. What will be the output of the following C code?


#include <stdio.h>
void main() {
int a[2][3] = {11, 22,33,4 4, 55};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}

a) Compile time error


b) 11 22 33 44 55 0
c) 11 22 33 44 55
d) Run time error

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

2. What will be the output of the following C code?


#include <stdio.h>
void main() {
int a[2][3] = {11, 22,33, ,4 4, 55};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}

a) Compile time error


b) 11 22 33 44 55 55
c) 11 22 33 44 55 0
d) Run time error

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. How would you implement the addition of two 2-D Matrices using a
2-D Array?
2. Can you explain the steps involved in performing the multiplication
of two 2-D Matrices?
3. How do you calculate the trace of a given 2-D Matrix? Provide an
example.
4. Describe the process of implementing the Transpose operation on a
2-D Matrix using suitable code or algorithm.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:
1. Brian W. Kernighan, Dennis M. Ritchie, “The C Programming Language:
ANSI C Version”, 2/e, Prentice-Hall/Pearson Education-2005.
2. E. Balagurusamy, “Programming in ANSI C” 4thed.,Tata McGraw-Hill
Education, 2008.
3. Mark Allen weiss, Data Structures and Algorithm Analysis in C, 2008,
Third Edition, Pearson Education.

Sites and Web links:


1. https://www.w3schools.in/c-programming/examples/matrix-
multiplication-in-c
2. https://www.w3schools.in/c-programming/examples/matrix-
CREATED BY K. VICTOR BABU
THANK YOU

Team – Computational Thinking For


Structured Desigh

CREATED BY K. VICTOR BABU

You might also like