Geek Logbook

Tech sea log book

Transforming a Matrix by Adding Numbers Around a Specific Value in Python

When working with matrices, we often need to perform transformations that update the values based on certain conditions. In this post, we’ll walk through a function that takes a matrix and updates it by adding numbers around a specific value, in this case, the number 9.

Problem Statement

Given a matrix (tablero), we want to return a new matrix where each cell’s value is incremented by 1 if it is adjacent (including diagonals) to a cell containing the number 9. For example, given the following input:

board = [
    [0, 0, 0],
    [0, 9, 0],
    [0, 0, 0]
]

We want to transform it into:

expected = [
    [1, 1, 1],
    [1, 9, 1],
    [1, 1, 1]
]

Solution

We can achieve this by creating a new matrix of the same size, iterating through each cell, and updating the surrounding cells whenever we encounter a 9.

Here’s the function to accomplish this:

def place_numbers(board):
    size = len(board)
    result_matrix = [[0] * size for _ in range(size)]

    for row in range(size):
        for column in range(size):
            if board[row][column] == 9:
                for i in range(max(0, row - 1), min(size, row + 2)):
                    for j in range(max(0, column - 1), min(size, column + 2)):
                        result_matrix[i][j] += 1
                result_matrix[row][column] = 9  # Keep the 9 in its place

    return result_matrix

Explanation

  1. Initialize the Result Matrix: We start by creating a new matrix result_matrix of the same size as the input board, filled with zeros.
size = len(board)
result_matrix = [[0] * size for _ in range(size)]

Iterate Through Each Cell: We loop through each cell in the input matrix board. If the cell contains a 9, we update the adjacent cells in result_matrix.

for row in range(size):
    for column in range(size):
        if board[row][column] == 9:
            for i in range(max(0, row - 1), min(size, row + 2)):
                for j in range(max(0, column - 1), min(size, column + 2)):
                    result_matrix[i][j] += 1
            result_matrix[row][column] = 9  # Keep the 9 in its place

Handle Edge Cases: The use of max and min functions ensures we do not go out of bounds when the 9 is on the border of the matrix.

for i in range(max(0, row - 1), min(size, row + 2)):
    for j in range(max(0, column - 1), min(size, column + 2)):
        result_matrix[i][j] += 1

Preserve the Original 9: Finally, we ensure that the 9 in the original matrix is preserved in the result_matrix.

result_matrix[row][column] = 9

Usage Example

Let’s test the function with our example:

board = [
    [0, 0, 0],
    [0, 9, 0],
    [0, 0, 0]
]

result = place_numbers(board)
for row in result:
    print(row)

The output will be:

[1, 1, 1]
[1, 9, 1]
[1, 1, 1]

This function efficiently updates the matrix as required, handling edge cases properly. It can be extended to handle larger matrices and different conditions based on your needs.

Conclusion

In this post, we demonstrated how to transform a matrix by incrementing the values around a specific number, handling edge cases, and ensuring the original matrix’s value is preserved. This technique can be useful in various applications, including game development and simulations.

Tags: