Notification texts go here Contact Us Click here

Matrix Multiplication Explanation with Program Using C Language.

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated
➤ Matrix Multiplication  :
 
let's perform a general matrix multiplication step by step.
 
Let'
s consider two matrices:
 
Matrix A:
```
| a  b |
| c  d |
```
 
Matrix B:
```
| e  f |
| g  h |
```
 
To perform the multiplication, we'll follow the standard matrix multiplication rules:
 
1. Take the dot product of the rows of Matrix A with the columns of Matrix B.
2. The resulting matrix will have dimensions (m x p), where m is the number of 
   rows in Matrix A, and p is the number of columns in Matrix B.
 
Step 1: Calculate the elements of the resulting matrix:
 
```
Element (1,1) of the resulting matrix:
( a * e ) + ( b * g ) = ae + bg
 
Element (1,2) of the resulting matrix:
( a * f ) + ( b * h ) = af + bh
 
Element (2,1) of the resulting matrix:
( c * e ) + ( d * g ) = ce + dg
 
Element (2,2) of the resulting matrix:
( c * f ) + ( d * h ) = cf + dh
```
 
Step 2: Place the results into the resulting matrix:
 
The resulting matrix will look like this:
```
| ae+bg  af+bh |
| ce+dg  cf+dh |
```
 
So, the final result of matrix multiplication for Matrix A * Matrix B is:
 
```
| ae+bg  af+bh |
| ce+dg  cf+dh |
```
 
Keep in mind that for matrix multiplication, the number of columns in the
first matrix must be equal to the number of rows in the second matrix.
If the matrices don'
t meet this condition, matrix multiplication is not possible.
 
Let's perform a 2x2 matrix multiplication as an example.
 
Let'
s consider two matrices:

 
Matrix A:
```
| 2  3 |
| 4  1 |
```
 
Matrix B:
```
| 5  1 |
| 6  2 |
```
 
To perform the multiplication, we'll follow the standard matrix multiplication rules:
 
1. Take the dot product of the rows of Matrix A with the columns of Matrix B.
2. The resulting matrix will have dimensions (m x p), where m is -
   - the number of rows in Matrix A, and p is the number of columns in Matrix B.
 
Let'
s calculate the result:
 
```
(2 * 5) + (3 * 6) = 10 + 18 = 28     | 28  ?
(4 * 5) + (1 * 6) = 20 + 6 = 26     | 26  ?
```
 
The resulting matrix will be:
```
| 28  ? |
| 26  ? |
```
 
So, the final 2x2 matrix multiplication result is:
```
| 28  8 |
| 26  6 |
```
 
Keep in mind that for matrix multiplication, the number of columns in 
the first matrix must be equal to the number of rows in the second matrix.
In this case, both matrices are 2x2, which satisfies this requirement.
If the matrices don't meet this condition, matrix multiplication is not possible.
  1.  ➤  Program to Perform Matrix Multiplication on Input matrix.
  2.  
  3. #include <stdio.h>
  4.  
  5. // Function to perform matrix multiplication
  6. void matrixMultiply(int matrix1[][100], int matrix2[][100], int result[][100],
  7.                     int m, int n, int p) {
  8.     int i, j, k;
  9.  
  10.     for (= 0; i < m; i++) {
  11.         for (= 0; j < p; j++) {
  12.             result[i][j] = 0;
  13.             for (= 0; k < n; k++) {
  14.                 result[i][j] += matrix1[i][k] * matrix2[k][j];
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. int main() {
  21.     int m, n, p, i, j;
  22.  
  23.     printf("Enter the number of rows (m) for Matrix A: ");
  24.     scanf("%d", &m);
  25.  
  26.     printf("Enter the number of columns (n) for Matrix A: ");
  27.     scanf("%d", &n);
  28.  
  29.     printf("Enter the number of columns (p) for Matrix B: ");
  30.     scanf("%d", &p);
  31.  
  32.     if (!= p) {
  33.         printf("Matrix multiplication is not possible. 
  34.                The number of columns in Matrix A must be equal to 
  35.                the number of rows in Matrix B.\n");
  36.         return 0;
  37.     }
  38.  
  39.     int matrix1[100][100], matrix2[100][100], result[100][100];
  40.  
  41.     // Input Matrix A
  42.     printf("Enter the elements of Matrix A:\n");
  43.     for (= 0; i < m; i++) {
  44.         for (= 0; j < n; j++) {
  45.             scanf("%d", &matrix1[i][j]);
  46.         }
  47.     }
  48.  
  49.     // Input Matrix B
  50.     printf("Enter the elements of Matrix B:\n");
  51.     for (= 0; i < n; i++) {
  52.         for (= 0; j < p; j++) {
  53.             scanf("%d", &matrix2[i][j]);
  54.         }
  55.     }
  56.  
  57.     // Perform matrix multiplication
  58.     matrixMultiply(matrix1, matrix2, result, m, n, p);
  59.  
  60.     // Print the resulting matrix
  61.     printf("Resulting Matrix:\n");
  62.     for (= 0; i < m; i++) {
  63.         for (= 0; j < p; j++) {
  64.             printf("%d ", result[i][j]);
  65.         }
  66.         printf("\n");
  67.     }
  68.  
  69.     return 0;
  70. }
  71.  
  72. OUTPUT :
  73.  
  74. Enter the number of rows (m) for Matrix A: 2      // store resulting matrix 
  75. Enter the number of columns (n) for Matrix A: 2
  76. Enter the number of columns (p) for Matrix B: 2
  77. Enter the elements of Matrix A:
  78. 2 3 4 5 
  79. Enter the elements of Matrix B:
  80. 4 5 6 7
  81. Resulting Matrix:
  82. 26 31 
  83. 46 55 
  84.  
  85.  
  86.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.