Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Matrix Multiplication | Working with 2D Arrays
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookMatrix 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

MatrixMultiplication.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
using 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:

  1. Check the dimensions: the first matrix (matrixA) has 3 columns, and the second matrix (matrixB) has 3 rows, which means multiplication is possible;
  2. Create a result matrix with as many rows as matrixA and as many columns as matrixB;
  3. 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;
  4. Assign the computed value to the appropriate cell in the result matrix;
  5. Print the result in a readable format.
IncompatibleMatrices.cs

IncompatibleMatrices.cs

copy
12345678910111213141516171819202122232425262728293031
using 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.

Note
Definition

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?

question mark

What are the requirements for multiplying two matrices?

Select the correct answer

question mark

How many nested loops are typically needed for matrix multiplication?

Select the correct answer

question mark

What happens if the matrices have incompatible sizes?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookMatrix 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

MatrixMultiplication.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
using 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:

  1. Check the dimensions: the first matrix (matrixA) has 3 columns, and the second matrix (matrixB) has 3 rows, which means multiplication is possible;
  2. Create a result matrix with as many rows as matrixA and as many columns as matrixB;
  3. 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;
  4. Assign the computed value to the appropriate cell in the result matrix;
  5. Print the result in a readable format.
IncompatibleMatrices.cs

IncompatibleMatrices.cs

copy
12345678910111213141516171819202122232425262728293031
using 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.

Note
Definition

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?

question mark

What are the requirements for multiplying two matrices?

Select the correct answer

question mark

How many nested loops are typically needed for matrix multiplication?

Select the correct answer

question mark

What happens if the matrices have incompatible sizes?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 5
some-alt