How to Check if a given Matrix is a Markov Matrix using Python?

In this article we are going to learn how to check whether the input matrix is a Markov Matrix using nested for loops and sum() function. Before that let us understand what is Markov Matrix with an example.

The matrix is said to be a Markov matrix if the sum of each row is equal to 1.

What is a Markov Matrix?

A Markov matrix (also called a stochastic matrix) is a square matrix where each row sums to 1. This property ensures that the matrix can represent probability transitions between states.

Example

matrix = [[0, 1, 0],
          [0, 0.3, 0.7],
          [0, 0, 1]]

# Check row sums:
# Row 1: 0 + 1 + 0 = 1
# Row 2: 0 + 0.3 + 0.7 = 1  
# Row 3: 0 + 0 + 1 = 1

print("Each row sum equals 1, so this is a Markov matrix")
Each row sum equals 1, so this is a Markov matrix

Method 1: Using Nested For Loops

The first approach manually calculates the sum of each row using nested loops to traverse through all elements.

def isMarkovMatrix(inputMatrix):
    # Traversing through the rows of a matrix
    for i in range(len(inputMatrix)):
        # Variable to store the sum of elements of current row
        currentrow_sum = 0
        # Traversing through all the columns of current row
        for j in range(len(inputMatrix[i])):
            # Getting the sum of current row
            currentrow_sum = currentrow_sum + inputMatrix[i][j]
        # Checking whether the sum of current row is not equal to 1
        if currentrow_sum != 1:
            # Returning false if sum is not equal to 1
            return False
    # Returning True if all rows sum to 1
    return True

# Input matrix
inputMatrix = [[0, 1, 0],
               [0, 0.3, 0.7],
               [0, 0, 1]]

if isMarkovMatrix(inputMatrix):
    print("The input matrix is a Markov matrix")
else:
    print("The input matrix is NOT a Markov matrix")
The input matrix is a Markov matrix

Method 2: Using sum() Function

A more concise approach using Python's built-in sum() function to calculate row sums directly.

def isMarkovMatrix(inputMatrix):
    for row in inputMatrix:
        rowSum = sum(row)
        if rowSum != 1:
            return False
    return True

# Input matrix
inputMatrix = [[0, 1, 0],
               [0, 0.3, 0.7],
               [0, 0, 1]]

if isMarkovMatrix(inputMatrix):
    print("The input matrix is a Markov matrix")
else:
    print("The input matrix is NOT a Markov matrix")
The input matrix is a Markov matrix

Testing with Non-Markov Matrix

Let's test our functions with a matrix that is NOT a Markov matrix ?

def isMarkovMatrix(inputMatrix):
    for row in inputMatrix:
        if sum(row) != 1:
            return False
    return True

# Non-Markov matrix (row sums are not 1)
nonMarkovMatrix = [[0.5, 0.3, 0.1],
                   [0.2, 0.4, 0.6],
                   [0.1, 0.2, 0.3]]

if isMarkovMatrix(nonMarkovMatrix):
    print("The input matrix is a Markov matrix")
else:
    print("The input matrix is NOT a Markov matrix")

# Check the row sums
for i, row in enumerate(nonMarkovMatrix):
    print(f"Row {i+1} sum: {sum(row)}")
The input matrix is NOT a Markov matrix
Row 1 sum: 0.8999999999999999
Row 2 sum: 1.2000000000000002
Row 3 sum: 0.6000000000000001

Comparison

Method Code Lines Readability Performance
Nested For Loops More Verbose Slightly slower
sum() Function Fewer Clean More efficient

Conclusion

Both methods effectively check if a matrix is Markovian by verifying that each row sums to 1. The sum() function approach is more concise and Pythonic, while the nested loop method provides explicit control over the summation process.

Updated on: 2026-03-27T12:50:35+05:30

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements