Matrix Multiplication
Matrix multiplication is a fundamental operation in mathematics and programming, especially when working with 2D arrays, also called matrices. To multiply two matrices, you must follow specific rules: the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result of multiplying an m x n matrix by an n x p matrix is a new m x p matrix. Each element in the resulting matrix is calculated by taking the dot product of a row from the first matrix and a column from the second matrix.
MatrixMultiplication.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758using System; namespace ConsoleApp { public class MatrixMultiplication { public static void Main(string[] args) { int[,] matrixA = { {1, 2, 3}, {4, 5, 6} }; int[,] matrixB = { {7, 8}, {9, 10}, {11, 12} }; int rowsA = matrixA.GetLength(0); int colsA = matrixA.GetLength(1); int rowsB = matrixB.GetLength(0); int colsB = matrixB.GetLength(1); if (colsA != rowsB) { Console.WriteLine("Cannot multiply: incompatible matrix sizes."); return; } int[,] result = new int[rowsA, colsB]; for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { int sum = 0; for (int k = 0; k < colsA; k++) { sum += matrixA[i, k] * matrixB[k, j]; } result[i, j] = sum; } } Console.WriteLine("Result of matrix multiplication:"); for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { Console.Write(result[i, j] + " "); } Console.WriteLine(); } } } }
To understand how matrix multiplication works, follow these steps based on the code above:
- Check the dimensions: the first matrix (
matrixA) has 3 columns, and the second matrix (matrixB) has 3 rows, which means multiplication is possible; - Create a result matrix with as many rows as
matrixAand as many columns asmatrixB; - Use three nested loops:
- The outer loop iterates over each row of the first matrix;
- The middle loop iterates over each column of the second matrix;
- The inner loop computes the sum of products for the current row and column, multiplying corresponding elements and summing them;
- Assign the computed value to the appropriate cell in the result matrix;
- Print the result in a readable format.
IncompatibleMatrices.cs
12345678910111213141516171819202122232425262728293031using System; namespace ConsoleApp { public class IncompatibleMatrices { public static void Main(string[] args) { int[,] matrixA = { {1, 2}, {3, 4} }; int[,] matrixB = { {5, 6, 7} }; int colsA = matrixA.GetLength(1); int rowsB = matrixB.GetLength(0); if (colsA != rowsB) { Console.WriteLine("Cannot multiply: the number of columns in the first matrix does not match the number of rows in the second matrix."); return; } // This code will not be reached because the matrices are incompatible. } } }
This example checks whether two matrices can be multiplied. It defines matrixA (2×2) and matrixB (1×3), then compares the number of columns in the first matrix with the number of rows in the second. Since these values do not match, the program prints an incompatibility message and stops. It demonstrates how to validate matrix dimensions before performing multiplication.
Matrix multiplication is the process of combining two matrices to produce a new matrix by multiplying rows of the first matrix by columns of the second matrix and summing the products.
1. What are the requirements for multiplying two matrices?
2. How many nested loops are typically needed for matrix multiplication?
3. What happens if the matrices have incompatible sizes?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain why the number of columns in the first matrix must match the number of rows in the second?
Can you show an example of multiplying two compatible matrices?
What happens if I try to multiply incompatible matrices?
Awesome!
Completion rate improved to 3.57
Matrix Multiplication
Sveip for å vise menyen
Matrix multiplication is a fundamental operation in mathematics and programming, especially when working with 2D arrays, also called matrices. To multiply two matrices, you must follow specific rules: the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result of multiplying an m x n matrix by an n x p matrix is a new m x p matrix. Each element in the resulting matrix is calculated by taking the dot product of a row from the first matrix and a column from the second matrix.
MatrixMultiplication.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758using System; namespace ConsoleApp { public class MatrixMultiplication { public static void Main(string[] args) { int[,] matrixA = { {1, 2, 3}, {4, 5, 6} }; int[,] matrixB = { {7, 8}, {9, 10}, {11, 12} }; int rowsA = matrixA.GetLength(0); int colsA = matrixA.GetLength(1); int rowsB = matrixB.GetLength(0); int colsB = matrixB.GetLength(1); if (colsA != rowsB) { Console.WriteLine("Cannot multiply: incompatible matrix sizes."); return; } int[,] result = new int[rowsA, colsB]; for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { int sum = 0; for (int k = 0; k < colsA; k++) { sum += matrixA[i, k] * matrixB[k, j]; } result[i, j] = sum; } } Console.WriteLine("Result of matrix multiplication:"); for (int i = 0; i < rowsA; i++) { for (int j = 0; j < colsB; j++) { Console.Write(result[i, j] + " "); } Console.WriteLine(); } } } }
To understand how matrix multiplication works, follow these steps based on the code above:
- Check the dimensions: the first matrix (
matrixA) has 3 columns, and the second matrix (matrixB) has 3 rows, which means multiplication is possible; - Create a result matrix with as many rows as
matrixAand as many columns asmatrixB; - Use three nested loops:
- The outer loop iterates over each row of the first matrix;
- The middle loop iterates over each column of the second matrix;
- The inner loop computes the sum of products for the current row and column, multiplying corresponding elements and summing them;
- Assign the computed value to the appropriate cell in the result matrix;
- Print the result in a readable format.
IncompatibleMatrices.cs
12345678910111213141516171819202122232425262728293031using System; namespace ConsoleApp { public class IncompatibleMatrices { public static void Main(string[] args) { int[,] matrixA = { {1, 2}, {3, 4} }; int[,] matrixB = { {5, 6, 7} }; int colsA = matrixA.GetLength(1); int rowsB = matrixB.GetLength(0); if (colsA != rowsB) { Console.WriteLine("Cannot multiply: the number of columns in the first matrix does not match the number of rows in the second matrix."); return; } // This code will not be reached because the matrices are incompatible. } } }
This example checks whether two matrices can be multiplied. It defines matrixA (2×2) and matrixB (1×3), then compares the number of columns in the first matrix with the number of rows in the second. Since these values do not match, the program prints an incompatibility message and stops. It demonstrates how to validate matrix dimensions before performing multiplication.
Matrix multiplication is the process of combining two matrices to produce a new matrix by multiplying rows of the first matrix by columns of the second matrix and summing the products.
1. What are the requirements for multiplying two matrices?
2. How many nested loops are typically needed for matrix multiplication?
3. What happens if the matrices have incompatible sizes?
Takk for tilbakemeldingene dine!