Python Program to Interchange Diagonals of Matrix
Last Updated :
24 Jul, 2022
Given a square matrix of order n*n, you have to interchange the elements of both diagonals.
Examples :
Input : matrix[][] = {1, 2, 3,
4, 5, 6,
7, 8, 9}
Output : matrix[][] = {3, 2, 1,
4, 5, 6,
9, 8, 7}
Input : matrix[][] = {4, 2, 3, 1,
5, 7, 6, 8,
9, 11, 10, 12,
16, 14, 15, 13}
Output : matrix[][] = {1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
11, 14, 15, 16}
Explanation : Idea behind interchanging diagonals of a square matrix is simple. Iterate from 0 to n-1 and for each iteration you have to swap a[i][i] and a[i][n-i-1].

Python3
# Python program to interchange
# the diagonals of matrix
N = 3;
# Function to interchange diagonals
def interchangeDiagonals(array):
# swap elements of diagonal
for i in range(N):
if (i != N / 2):
temp = array[i][i];
array[i][i] = array[i][N - i - 1];
array[i][N - i - 1] = temp;
for i in range(N):
for j in range(N):
print(array[i][j], end = " ");
print();
# Driver Code
if __name__ == '__main__':
array = [ 4, 5, 6 ],[ 1, 2, 3 ],[ 7, 8, 9 ];
interchangeDiagonals(array);
# This code is contributed by Rajput-Ji
Output:
6 5 4
1 2 3
9 8 7
Time Complexity: O(N) where N is no of rows or columns; as we are using single loop for interchanging diagonals of a given matrix.
Auxiliary Space: O(1), as we are not using any extra space.
Please refer complete article on Program to Interchange Diagonals of Matrix for more details!
Similar Reads
Python sympy | Matrix.diagonalize() method With the help of sympy.Matrix().diagonalize() method, we can diagonalize a matrix. diagonalize() returns a tuple (P, D), where D is diagonal and M = PDP^{-1}. Syntax: Matrix().diagonalize() Returns: Returns a tuple of matrix where the second element represents the diagonal of the matrix. Example #1:
1 min read
Python Program for Program to Print Matrix in Z form Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Python3 # Python program to print
2 min read
Python | numpy.fill_diagonal() method With the help of numpy.fill_diagonal() method, we can get filled the diagonals of numpy array with the value passed as the parameter in numpy.fill_diagonal() method. Syntax : numpy.fill_diagonal(array, value) Return : Return the filled value in the diagonal of an array. Example #1 : In this example
1 min read
How to reverse column order in a matrix with Python? In this article, we will see how to reverse the column order of a matrix in Python. Examples: Input: arr = [[10,20,30], [40,50,60], [70,80,90]] Output: 30 20 10 60 50 40 90 80 70 Input: arr = [[15,30], [45,60], [75,90], [105,120]] Output: 30 15 60 45 90 75 120 105 Matrices are created in python by u
2 min read
AlgebraOnMatrix Module in Python AlgebraOnMatrix module is that library of python which helps you to perform basic matrix algebra such as addition of two matrices, subtraction of two matrices, multiplication of two matrices, transpose of matrix . Installing Library This module does not come built-in with Python. You need to install
3 min read