➤ 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.
- ➤ Program to Perform Matrix Multiplication on Input matrix.
- #include <stdio.h>
- // Function to perform matrix multiplication
- void matrixMultiply(int matrix1[][100], int matrix2[][100], int result[][100],
- int m, int n, int p) {
- int i, j, k;
- for (i = 0; i < m; i++) {
- for (j = 0; j < p; j++) {
- result[i][j] = 0;
- for (k = 0; k < n; k++) {
- result[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- }
- int main() {
- int m, n, p, i, j;
- printf("Enter the number of rows (m) for Matrix A: ");
- scanf("%d", &m);
- printf("Enter the number of columns (n) for Matrix A: ");
- scanf("%d", &n);
- printf("Enter the number of columns (p) for Matrix B: ");
- scanf("%d", &p);
- if (n != p) {
- printf("Matrix multiplication is not possible.
- The number of columns in Matrix A must be equal to
- the number of rows in Matrix B.\n");
- return 0;
- }
- int matrix1[100][100], matrix2[100][100], result[100][100];
- // Input Matrix A
- printf("Enter the elements of Matrix A:\n");
- for (i = 0; i < m; i++) {
- for (j = 0; j < n; j++) {
- scanf("%d", &matrix1[i][j]);
- }
- }
- // Input Matrix B
- printf("Enter the elements of Matrix B:\n");
- for (i = 0; i < n; i++) {
- for (j = 0; j < p; j++) {
- scanf("%d", &matrix2[i][j]);
- }
- }
- // Perform matrix multiplication
- matrixMultiply(matrix1, matrix2, result, m, n, p);
- // Print the resulting matrix
- printf("Resulting Matrix:\n");
- for (i = 0; i < m; i++) {
- for (j = 0; j < p; j++) {
- printf("%d ", result[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
- OUTPUT :
- Enter the number of rows (m) for Matrix A: 2 // store resulting matrix
- Enter the number of columns (n) for Matrix A: 2
- Enter the number of columns (p) for Matrix B: 2
- Enter the elements of Matrix A:
- 2 3 4 5
- Enter the elements of Matrix B:
- 4 5 6 7
- Resulting Matrix:
- 26 31
- 46 55