Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Interchange Diagonals of Matrix using Python
In this article, we will learn how to interchange the main diagonal and anti-diagonal elements of a square matrix using Python. This operation swaps elements at position (i,i) with elements at position (i, n-1-i) for each row.
We will explore two different approaches to accomplish this task using a square NxN matrix.
Methods Used
The following methods will be demonstrated ?
Using Nested For Loops with Temporary Variable
Using Tuple Swapping
Algorithm
Following are the steps to interchange matrix diagonals ?
Create a function to print the matrix in a readable format
Create a function to swap diagonal elements
Traverse through each row of the matrix
For each row i, swap matrix[i][i] with matrix[i][n-1-i]
Skip the middle element for odd-sized matrices
Method 1: Using Temporary Variable
This method uses a temporary variable to swap diagonal elements ?
def print_matrix(matrix):
"""Print the matrix in a readable format"""
for row in matrix:
for element in row:
print(element, end=" ")
print()
def swap_diagonals_temp(matrix):
"""Swap diagonals using temporary variable"""
n = len(matrix)
for i in range(n):
# Skip middle element for odd-sized matrices
if i != n // 2:
# Swap main diagonal with anti-diagonal
temp = matrix[i][i]
matrix[i][i] = matrix[i][n - 1 - i]
matrix[i][n - 1 - i] = temp
# Input matrix (3x3)
matrix = [[5, 1, 3],
[6, 10, 8],
[7, 2, 4]]
print("Original Matrix:")
print_matrix(matrix)
print("After Interchanging Diagonals:")
swap_diagonals_temp(matrix)
print_matrix(matrix)
Original Matrix: 5 1 3 6 10 8 7 2 4 After Interchanging Diagonals: 3 1 5 6 10 8 4 2 7
Method 2: Using Tuple Swapping
Python allows swapping variables without a temporary variable using tuple unpacking ?
def print_matrix(matrix):
"""Print the matrix in a readable format"""
for row in matrix:
for element in row:
print(element, end=" ")
print()
def swap_diagonals_tuple(matrix):
"""Swap diagonals using tuple swapping"""
n = len(matrix)
for i in range(n):
# Skip middle element for odd-sized matrices
if i != n // 2:
# Swap using tuple unpacking
matrix[i][i], matrix[i][n - 1 - i] = matrix[i][n - 1 - i], matrix[i][i]
# Input matrix (4x4 for demonstration)
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
print("Original Matrix:")
print_matrix(matrix)
print("After Interchanging Diagonals:")
swap_diagonals_tuple(matrix)
print_matrix(matrix)
Original Matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 After Interchanging Diagonals: 4 2 3 1 5 7 6 8 9 11 10 12 16 14 15 13
Comparison
| Method | Space Usage | Readability | Time Complexity |
|---|---|---|---|
| Temporary Variable | O(1) extra space | Clear and explicit | O(N) |
| Tuple Swapping | O(1) | More Pythonic | O(N) |
Conclusion
Both methods efficiently interchange matrix diagonals in O(N) time complexity. The tuple swapping method is more Pythonic and concise, while the temporary variable approach is more explicit and easier to understand for beginners.
