Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Addition of Matrices
1.1.
Algorithm
1.2.
Program
2.
Subtraction of Matrices
2.1.
Algorithm
2.2.
Program
3.
Multiplication of Matrices
3.1.
Algorithm
3.2.
Program
4.
FAQs
5.
Key Takeaway’s
Last Updated: Mar 27, 2024
Easy

Operations On Matrices

Author Prachi Singh
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Addition of Matrices

The addition of two matrices, Xm*n and Ym*n, gives a resultant matrix  Zm*n. The elements in the resulting matrix Z is the addition of the corresponding elements of two matrices.

Algorithm

The algorithm for the addition of two matrices can be described as

 

for i in 1 to m
   for j in 1 to n
      zij = xij + yij

Program

// C++ Program for matrix addition
 
#include <iostream>
using namespace std;
 
int main()
{
 
    int n = 2, m = 2;
    int y[n][m] = { { 3, 2 }, { 2, 3 } };
    int z[n][m] = { { 1, 2 }, { 2, 1 } };
 
    int x[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            z[i][j] = x[i][j] + y[i][j];
        }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << z[i][j] << " ";
        cout << endl;
    }
}

Output:

 

4  4
4   4

 

Time Complexity:  O(n * m)

Auxiliary Space:  O(n * m)

Subtraction of Matrices

The subtraction of two matrices, Xm*n and Ym*n, gives a resultant matrix  Zm*n. The elements in the resulting matrix Z is the subtraction of the corresponding elements of two matrices.

Algorithm

The algorithm for the subtraction of two matrices can be described as

 

for i in 1 to m
   for j in 1 to n
      zij = xij-yij

 

 

Program

// C++ Program for matrix subtraction
 
#include <iostream>
using namespace std;
 
int main()
{
 
    int n = 2, m = 2;
    int x[n][m] = { { 4, 8 }, { 3, 8 } };
    int y[n][m] = { { 3, 6 }, { 2, 1 } };
 
    int z[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            z[i][j] = x[i][j] - y[i][j];
        }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << z[i][j] << " ";
        cout << endl;
    }
}

Output:

1  2
1  7

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Multiplication of Matrices

The multiplication of two matrices Xm*n and Yn*p gives a matrix Zm*p. It means number of columns in X must be equal to number of rows in Y to calculate Z=X*Y

Algorithm

The algorithm for the multiplication of two matrices can be described as

for i in 1 to m
   for j in 1 to p
      zij = 0
      for k in 1 to n
         zij += xik*ykj

 

Program

// C++ Program for matrix Multiplication
 
#include <iostream>
using namespace std;
 
int main()
{
 
    int n = 2, m = 2;
    int x[n][m] = { { 1, 2 }, { 1, 3 } };
    int y[n][m] = { { 3, 4 }, { 2, 3 } };
 
    int z[n][m];
    int i, j, k;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            z[i][j] = 0;
            for (k = 0; k < n; k++)
                z[i][j] += x[i][k] * y[k][j];
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cout << z[i][j] << " ";
        cout << endl;
    }
}.

Output:

7  10
9   13

 

FAQs

 

1. What are the different operations that can be performed on matrices?

The different operations that can be performed on matrices are addition, subtraction and multiplication.

 

2. What is the complexity of subtraction operation?

The complexity of subtraction operation is O(m*n).

 

3. What is the complexity of addition operation?

The complexity of addition operation is O(m*n).

 

4. What is the complexity of multiplication operation?

The complexity of multiplication operation is O(m*n*p) where m*n and n*p are order of X and Y respectively.

Key Takeaway’s

Congratulations on finishing the blog!! After reading this blog, you will grasp the concept of the Operation of Matrices.

If you are preparing yourself for the top tech companies, don't worry. Coding Ninjas has your back. Visit this link for a well-defined and structured material, which will help you provide access to knowledge in every domain.

Check out this problem - Matrix Median

Previous article
Types of Matrix
Next article
Rank of Matrix
Live masterclass