Q:

Program to find the product of two matrices

belongs to collection: Matrix Programs

0

Explanation

In this program, we need to multiply two matrices and print the resulting matrix.

Product of two matrices

The product of two matrices can be computed by multiplying elements of the first row of the first matrix with the first column of the second matrix then, add all the product of elements. Continue this process until each row of the first matrix is multiplied with each column of the second matrix

Consider above example, first element in resulting matrix prod[0,0] can be computed by multiplying first row of first matrix i.e. (1, 3, 2) with first column of second matrix i.e. (2, 1, 1) and finally sum all the product of elements i.e. (1*2) + (3*1) + (2*1) = 7. Similarly, second entry prod[0,1] can be computed by multiplying the first row of the first matrix with the second column of the second matrix and sum all the product.

Two matrices can be multiplied if and only if they satisfy the following condition:

the number of columns present in the first matrix should be equal to the number of rows present in the second matrix.

Suppose dimension of matrix A is p × q and matrix B is q × r, then the dimension of resulting matrix will be p × r. Matrix multiplication can be represented as

Cij = Σ AikBkj

Algorithm

  1. Declare and initialize two two-dimensional arrays a and b.
  2. Calculate the number of rows and columns present in the array a and store it in variables row1 and col1 respectively.
  3. Calculate the number of rows and columns present in the array b and store it in variables row2 and col2 respectively.
  4. Check if col1 is equal to row2. For two matrices to be multiplied, the number of column in the first matrix must be equal to the number of rows in the second matrix.
  5. If col1 is not equal to row2, display the message "Matrices cannot be multiplied."
  6. If they are equal, loop through the arrays a and b by multiplying elements of the first row of the first matrix with the first column of the second matrix and add all the product of elements.
    e.g prod11 = a11 x b11 + a11 x b21 + a11 x b31
  7. Repeat the previous step till all the rows of the first matrix is multiplied with all the columns of the second matrix.
  8. Display the elements of array prod.

Input:

Matrix a = [1, 3, 2]  

           [3, 1, 1]  

           [1, 2, 2]  

matrix b = [2, 1, 1]  

           [1, 0, 1]  

           [1, 3, 1]  

Output:

Product of two matrices: [7 7 6]
                         [8 6 5]
                         [6 7 5]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#Initialize matrix a  
a = [  
        [1, 3, 2],  
        [3, 1, 1],  
        [1, 2, 2]  
    ];  
   
#Initialize matrix b  
b = [  
          [2, 1, 1],  
         [1, 0, 1],  
         [1, 3, 1]  
     ];  
   
#Calculates number of rows and columns present in first matrix  
row1 = len(a);  
col1 = len(a[0]);  
   
#Calculates number of rows and columns present in second matrix  
row2 = len(b);  
col2 = len(b[0]);  
   
#For two matrices to be multiplied,   
#number of columns in the first matrix must be equal to the number of rows in the second matrix  
  
if(col1 != row2):  
    print("Matrices cannot be multiplied");  
   
else:  
    #Array prod will hold the result and is initialized with zeroes.  
    prod = [[0]*row1 for i in range(col2)];  
      
    #Performs product of matrices a and b. Store the result in matrix prod  
    for i in range(0, row1):  
        for j in range(0, col2):  
            for k in range(0, row2):  
                prod[i][j] = prod[i][j] + a[i][k] * b[k][j];   
                  
    print("Product of two matrices: ");  
    for i in range(0, row1):  
        for j in range(0, col2):  
            print(prod[i][j]),  
              
        print(" ");  

 

Output:

Product of two matrices: 
7 7 6 
8 6 5 
6 7 5 

 

C

#include <stdio.h>  
   
int main()  
{  
    int row1, col1, row2, col2;  
          
    //Initialize matrix a  
    int a[][3] = {  
                    {1, 3, 2},  
                    {3, 1, 1},  
                    {1, 2, 2}  
                 };  
      
    //Initialize matrix b  
    int b[][3] = {  
                      {2, 1, 1},  
                     {1, 0, 1},  
                     {1, 3, 1}  
                 };  
      
    //Calculates number of rows and columns present in first matrix  
    row1 = (sizeof(a)/sizeof(a[0]));  
    col1 = (sizeof(a)/sizeof(a[0][0]))/row1;  
      
    //Calculates number of rows and columns present in second matrix  
    row2 = (sizeof(b)/sizeof(b[0]));  
    col2 = (sizeof(b)/sizeof(b[0][0]))/row2;  
      
    //For two matrices to be multiplied,   
    //number of columns in first matrix must be equal to number of rows in second matrix  
    if(col1 != row2){  
        printf("Matrices cannot be multiplied \n");  
    }  
    else{  
        //Array prod will hold the result  
        int prod[row1][col2];  
          
        //Performs product of matrices a and b. Store the result in matrix prod  
        for(int i = 0; i < row1; i++){  
            for(int j = 0; j < col2; j++){  
                prod[i][j] = 0;  
                for(int k = 0; k < row2; k++){  
                   prod[i][j] = prod[i][j] + a[i][k] * b[k][j];   
                }  
            }  
        }  
          
        printf("Product of two matrices: \n");  
        for(int i = 0; i < row1; i++){  
            for(int j = 0; j < col2; j++){  
               printf("%d ", prod[i][j]);  
            }  
            printf("\n");  
        }  
    }  
   
    return 0;  
}  

 

Output:

Product of two matrices: 
7 7 6 
8 6 5 
6 7 5 

 

JAVA

public class ProdMatrix  
{  
    public static void main(String[] args) {  
        int row1, col1, row2, col2;  
          
        //Initialize matrix a  
          int a[][] = {  
                          {1, 3, 2},  
                          {3, 1, 1},  
                          {1, 2, 2}  
                       };  
            
          //Initialize matrix b  
          int b[][] = {  
                          {2, 1, 1},  
                         {1, 0, 1},  
                         {1, 3, 1}  
                     };  
            
          //Calculates number of rows and columns present in first matrix  
          row1 = a.length;  
        col1 = a[0].length;  
          
        //Calculates the number of rows and columns present in the second matrix  
  
          row2 = b.length;  
        col2 = b[0].length;  
          
        //For two matrices to be multiplied,   
        //number of columns in first matrix must be equal to number of rows in second matrix  
        if(col1 != row2){  
            System.out.println("Matrices cannot be multiplied");  
        }  
        else{  
            //Array prod will hold the result  
            int prod[][] = new int[row1][col2];  
              
            //Performs product of matrices a and b. Store the result in matrix prod  
            for(int i = 0; i < row1; i++){  
                for(int j = 0; j < col2; j++){  
                    for(int k = 0; k < row2; k++){  
                       prod[i][j] = prod[i][j] + a[i][k] * b[k][j];   
                    }  
                }  
            }  
              
            System.out.println("Product of two matrices: ");  
            for(int i = 0; i < row1; i++){  
                for(int j = 0; j < col2; j++){  
                   System.out.print(prod[i][j] + " ");  
                }  
                System.out.println();  
            }  
        }  
    }  
}  

 

Output:

Product of two matrices: 
7 7 6 
8 6 5 
6 7 5 

 

C#

 using System;  
                      
public class ProdMatrix  
{  
    public static void Main()  
    {  
        int row1, col1, row2, col2;  
          
        //Initialize matrix a  
          int[,] a = {  
                          {1, 3, 2},  
                          {3, 1, 1},  
                          {1, 2, 2}  
                     };  
            
          //Initialize matrix b  
          int[,] b = {  
                          {2, 1, 1},  
                         {1, 0, 1},  
                         {1, 3, 1}  
                     };  
            
          //Calculates number of rows and columns present in first matrix  
          row1 = a.GetLength(0);  
        col1 = a.GetLength(1);  
          
        //Calculates the number of rows and columns present in the second matrix  
  
          row2 = b.GetLength(0);  
        col2 = b.GetLength(1);  
          
        //For two matrices to be multiplied,   
        //number of columns in first matrix must be equal to number of rows in second matrix  
        if(col1 != row2){  
            Console.WriteLine("Matrices cannot be multiplied");  
        }  
        else{  
            //Array prod will hold the result  
            int[,] prod = new int[row1,col2];  
              
            //Performs product of matrices a and b. Store the result in matrix prod  
            for(int i = 0; i < row1; i++){  
                for(int j = 0; j < col2; j++){  
                    for(int k = 0; k < row2; k++){  
                       prod[i,j] = prod[i,j] + a[i,k] * b[k,j];   
                    }  
                }  
            }  
              
            Console.WriteLine("Product of two matrices: ");  
            for(int i = 0; i < row1; i++){  
                for(int j = 0; j < col2; j++){  
                   Console.Write(prod[i,j] + " ");  
                }  
                Console.WriteLine();  
            }  
        }  
    }  
}  

 

Output:

Product of two matrices: 
7 7 6 
8 6 5 
6 7 5 

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize matrix a  
$a = array(  
            array(1, 3, 2),  
            array(3, 1, 1),  
            array(1, 2, 2)  
          );  
   
//Initialize matrix b  
$b = array(  
              array(2, 1, 1),  
             array(1, 0, 1),  
             array(1, 3, 1)  
           );  
   
//Calculates number of rows and columns present in first matrix  
$row1 = count($a);  
$col1 = count($a[0]);  
   
//Calculates number of rows and columns present in second matrix  
$row2 = count($b);  
$col2 = count($b[0]);  
   
//For two matrices to be multiplied,   
//number of columns in first matrix must be equal to number of rows in second matrix  
if($col1 != $row2){  
    print("Matrices cannot be multiplied <br>");  
}  
else{  
    //Array prod will hold the result and initialize it with 0  
    $prod = array_fill(0, $col2, array_fill(0, $row1, 0));  
      
    //Performs product of matrices a and b. Store the result in matrix prod  
    for($i = 0; $i < $row1; $i++){  
        for($j = 0; $j < $col2; $j++){  
            for($k = 0; $k < $row2; $k++){  
               $prod[$i][$j] = $prod[$i][$j] + $a[$i][$k] * $b[$k][$j];   
            }  
        }  
    }  
      
    print("Product of two matrices: <br>");  
    for($i = 0; $i < $row1; $i++){  
        for($j = 0; $j < $col2; $j++){  
          print($prod[$i][$j] . " ");  
        }  
        print("<br>");  
    }  
}  
?>  
</body>  
</html>  

 

Output:

Product of two matrices: 
7 7 6 
8 6 5 
6 7 5 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to find the sum of each row and each colum... >>
<< Program to find the frequency of odd & even number...