Tutorialspoint
Problem
Solution
Submissions

Perform Matrix Multiplication.

Certification: Advanced Level Accuracy: 100% Submissions: 2 Points: 15

Write a Python program that performs matrix multiplication on two given matrices.

Example 1
  • Input 1: [[1, 2], [3, 4]]
  • Input 2: [[5, 6], [7, 8]]
  • Output: [[19, 22], [43, 50]]
  • Explanation:
    • Step 1: Take the input matrices.
    • Step 2: Perform matrix multiplication.
    • Step 3: Return the resulting matrix.
Example 2
  • Input 1: [[1, 2, 3], [4, 5, 6]]
  • Input 2: [[7, 8], [9, 10], [11, 12]]
  • Output: [[58, 64], [139, 154]]
  • Explanation:
    • Step 1: Take the input matrices.
    • Step 2: Perform matrix multiplication.
    • Step 3: Return the resulting matrix.
Constraints
  • The number of columns in matrix1 must equal the number of rows in matrix2.
  • 1 ≤ rows, columns ≤ 100
  • Matrix elements are integers.
  • Time Complexity: O(n^3), where n is the size of the matrices.
  • Space Complexity: O(n^2)
ArraysFacebookWipro
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use nested loops to iterate through rows of the first matrix and columns of the second matrix.
  • Multiply corresponding elements and sum them up to get the result matrix.
  • Ensure the dimensions of the matrices are compatible for multiplication.

Steps to solve by this approach:

 Step 1: Verify that matrices are compatible for multiplication (cols1 = rows2).
 Step 2: Initialize a result matrix of dimensions rows1 × cols2 with zeros.
 Step 3: Use three nested loops to compute each element of the result matrix.
 Step 4: For each element result[i][j], compute the dot product of row i from matrix1 and column j from matrix2.
 Step 5: Return the final result matrix.

Submitted Code :