Python Program to Multiply to Matrix Using Multi-dimensional Arrays

A matrix is a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an m × n matrix and m and n are called its dimensions. In Python, matrices can be created using lists or NumPy arrays.

Matrix multiplication can be done in two ways: standard matrix multiplication (dot product) where the number of columns in the first matrix must equal the number of rows in the second matrix, and element-wise multiplication where matrices must have the same dimensions.

Matrix Multiplication Types

For standard matrix multiplication, if we have matrices A (2×3) and B (3×2), the result will be a 2×2 matrix ?

[a1, a2, a3] [b1, b2] [a1*b1+a2*b3+a3*b5, a1*b2+a2*b4+a3*b6] [a4, a5, a6] × [b3, b4] = [a4*b1+a5*b3+a6*b5, a4*b2+a5*b4+a6*b6] [b5, b6] Standard Matrix Multiplication (Dot Product)

For element-wise multiplication, matrices must have identical dimensions ?

[a11, a12] [b11, b12] [a11*b11, a12*b12] [a21, a22] × [b21, b22] = [a21*b21, a22*b22] Element-wise Multiplication

Using Nested For Loops

We can implement standard matrix multiplication using nested loops to iterate through rows and columns ?

# Define matrices using multidimensional arrays
matrix_a = [[1, 2, 3],
            [4, 1, 2],
            [2, 3, 1]]
 
matrix_b = [[1, 2, 3, 2],
            [2, 3, 6, 3],
            [3, 1, 4, 2]]

def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display input matrices
print('The first matrix is:') 
display(matrix_a)
print('The second matrix is:')
display(matrix_b)

# Initialize result matrix with zeros
result = [[0 for j in range(len(matrix_b[0]))] for i in range(len(matrix_a))]

# Multiply matrices using nested loops
for i in range(len(matrix_a)):
    for j in range(len(matrix_b[0])):
        for k in range(len(matrix_b)):        
            result[i][j] += matrix_a[i][k] * matrix_b[k][j]

print('The multiplication result is:')
display(result)
The first matrix is:
[1, 2, 3]
[4, 1, 2]
[2, 3, 1]

The second matrix is:
[1, 2, 3, 2]
[2, 3, 6, 3]
[3, 1, 4, 2]

The multiplication result is:
[14, 11, 27, 14]
[12, 13, 26, 15]
[11, 14, 28, 15]

Using NumPy @ Operator

NumPy provides the @ operator for matrix multiplication, which is more efficient and readable ?

import numpy as np

# Define matrices using NumPy arrays
matrix_a = np.array([[1, 2, 5], 
                     [1, 0, 6], 
                     [9, 8, 0]])

matrix_b = np.array([[0, 3, 5], 
                     [4, 6, 9], 
                     [1, 8, 0]])

print('First matrix:')
print(matrix_a)
print('\nSecond matrix:')
print(matrix_b)

# Matrix multiplication using @ operator
result = matrix_a @ matrix_b

print('\nMatrix multiplication result:')
print(result)
First matrix:
[[1 2 5]
 [1 0 6]
 [9 8 0]]

Second matrix:
[[0 3 5]
 [4 6 9]
 [1 8 0]]

Matrix multiplication result:
[[ 13  55  23]
 [  6  51   5]
 [ 32  75 117]]

Element-wise Multiplication

For element-wise multiplication, use the * operator with NumPy arrays ?

import numpy as np

# Define matrices with same dimensions
matrix_a = np.array([[1, 2, 5], 
                     [1, 0, 6], 
                     [9, 8, 0]])

matrix_b = np.array([[0, 3, 5], 
                     [4, 6, 9], 
                     [1, 8, 0]])

print('First matrix:')
print(matrix_a)
print('\nSecond matrix:')
print(matrix_b)

# Element-wise multiplication
result = matrix_a * matrix_b

print('\nElement-wise multiplication result:')
print(result)
First matrix:
[[1 2 5]
 [1 0 6]
 [9 8 0]]

Second matrix:
[[0 3 5]
 [4 6 9]
 [1 8 0]]

Element-wise multiplication result:
[[ 0  6 25]
 [ 4  0 54]
 [ 9 64  0]]

Comparison

Method Syntax Type Requirements
Nested Loops Manual implementation Standard multiplication Columns of A = Rows of B
@ Operator A @ B Standard multiplication Columns of A = Rows of B
* Operator A * B Element-wise Same dimensions

Conclusion

Use nested loops for learning matrix multiplication concepts. For practical applications, NumPy's @ operator provides efficient standard matrix multiplication, while * performs element-wise operations.

Updated on: 2026-03-27T06:40:02+05:30

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements