Interchange Diagonals of Matrix using Python


In this article, we will learn a python program to interchange the diagonals of the matrix.

Assume we have taken an NxN input matrix. We will now interchange the diagonals of an input matrix using the below methods.

Methos Used

The following are the various methods to accomplish this task −

  • Using the Nested For Loops with Temporary Variable

  • Using ‘,’ Swapping Operator

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a variable to store the input number of rows of a matrix

  • Create a function printGivenMatrix() that prints the given matrix.

  • Inside the printGivenMatrix() function Traverse through the rows of the given Matrix using For loop.

  • Using another Nested For loop traverse through the columns of the current row.

  • Print the corresponding matrix element at the current row and column.

  • Create a function swapMatDiagonals() to interchange the diagonals of an input matrix by accepting the input matrix as an argument.

  • Use the for loop, to traverse through the rows of a matrix.

  • Use the if conditional statement to check whether the row index is not equal to the half of rows of the matrix.

  • Swap the elements of diagonals using a temporary variable if the condition is true.

  • Create a variable to store the input matrix.

  • Call the above-defined swapMatDiagonals() function by passing the input matrix as an argument to interchange the diagonals of a matrix.

  • Print the input Matrix again by calling the printGivenMatrix() method.

Method 1:Using the Nested For Loops and Temporary Variable

Example

The following program interchanges the diagonals of a matrix using the nested for loop and temporary variable −

# creating a function to print the given matrix
def printGivenMatrix(inputMatrix):
   # Traversing in the rows of the input matrix
      for p in range(rows):
         # Traversing in the columns corresponding to the current row
         # of the input matrix
            for q in range(rows):
               # printing the element at the current row and column
                  print(inputMatrix[p][q], end=" ")
            # Printing a new line to separate the rows
            print()
# creating a function to interchange the diagonals
# of matrix by accepting input matrix as an argument
def swapMatDiagonals(inputMatrix):
   # traversing through the rows of a matrix
      for p in range(rows):
         # checking whether the row index is not half of the rows of a matrix
         if (p != rows / 2):
            # swapping elements of diagonals using a temporary variable
               tempVariable = inputMatrix[p][p]
               inputMatrix[p][p] = inputMatrix[p][rows - p - 1]
               inputMatrix[p][rows - p - 1] = tempVariable
# input no of rows of a matrix
rows = 3
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
               [6, 10, 8],
               [7, 2, 4]]
print("The Given Matrix is:")
printGivenMatrix(inputMatrix)
print("Interchanging Diagonals of an input matrix:")
#calling the above swapMatDiagonals() function bypassing the input matrix to it
swapMatDiagonals(inputMatrix)
# Printing the matrix again after interchanging the diagonals of it
printGivenMatrix(inputMatrix)

Output

On execution, the above program will generate the following output −

The Given Matrix is:
5 1 3 
6 10 8 
7 2 4 
Interchanging Diagonals of an input matrix:
3 1 5 
6 10 8 
4 2 7 

Time Complexity − O(N)

where N represents the number of rows or columns, as we only use one loop to swap the diagonals of a specified matrix.

Auxiliary Space − O(1). Since we are not using any additional space.

Method 2: Using ‘,’ Swapping Operator

Example

The following program interchanges the diagonals of a matrix using the ‘,’(Swapping) operator −

# creating a function to print the given matrix
def printGivenMatrix(inputMatrix):
   # Traversing in the rows of the input matrix
      for p in range(rows):
         # Traversing in the columns corresponding to the current row
            for q in range(rows):
               # printing the element at the current row and column
                  print(inputMatrix[p][q], end=" ")
            # Printing a new line to separate the rows
            print()
# creating a function to interchange the diagonals of the matrix
def swapMatDiagonals(inputMatrix):
   # traversing through the rows of a matrix
      for p in range(rows):
         # checking whether the row index is not half of the rows of a matrix
         if (p != rows / 2):
            # swapping elements of diagonals using ','(swapping operator)
               inputMatrix[p][p], inputMatrix[p][rows - p - 1] =  inputMatrix[p][rows - p - 1], inputMatrix[p][p]
# input no of rows of a matrix
rows = 3
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
               [6, 10, 8],
               [7, 2, 4]]
print("The Given Matrix is:")
printGivenMatrix(inputMatrix)
print("Interchanging Diagonals of an input matrix:")
# calling the above swapMatDiagonals() function
swapMatDiagonals(inputMatrix)
# Printing the matrix again after interchanging the diagonals of it
printGivenMatrix(inputMatrix)

Output

On execution, the above program will generate the following output −

The Given Matrix is:
5 1 3 
6 10 8 
7 2 4 
Interchanging Diagonals of an input matrix:
3 1 5 
6 10 8 
4 2 7 

Time Complexity − O(N)

Auxiliary Space − O(1). Since we are not using any additional space.

Conclusion

In this article, we learned two different ways to swap the matrix's diagonal elements. Additionally, we learned how to swap two variables without taking up extra space by using the swapping operator (','). (temporary variable)

Updated on: 27-Jan-2023

470 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements