{"id":20801,"date":"2021-09-21T09:00:26","date_gmt":"2021-09-21T03:30:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=20801"},"modified":"2021-11-22T18:36:16","modified_gmt":"2021-11-22T13:06:16","slug":"python-program-to-swap-major-and-minor-diagonals-of-a-square-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-swap-major-and-minor-diagonals-of-a-square-matrix\/","title":{"rendered":"Python Program to Swap Major and Minor Diagonals of a Square Matrix"},"content":{"rendered":"

In the previous article, we have discussed Python Program for Exponential Squaring (Fast Modulo Multiplication)<\/a><\/p>\n

Given a square matrix, the task is to swap the major diagonal elements and minor diagonal elements of a given matrix.<\/p>\n

What is a matrix:<\/strong><\/p>\n

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n

Diagonal Matrix:<\/strong><\/p>\n

The entries outside the main diagonal of a diagonal matrix are all 0; the word usually refers to square matrices.<\/p>\n

Major Diagonal Matrix :<\/strong><\/p>\n

The Major Diagonal Elements of a Matrix are those that occur from the top left corner of the matrix down to the bottom right corner. The Major Diagonal is also referred to as the Main Diagonal or the Primary Diagonal.<\/p>\n

Minor Diagonal Matrix :<\/strong><\/p>\n

Minor Diagonal Elements are those that appear from the top right corner of the matrix down to the bottom left corner. Secondary Diagonal is another name for it.<\/p>\n

Example:<\/strong><\/p>\n

\"\"<\/p>\n

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n

In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Matrix : \r\n6 2 0\r\n1 4 2\r\n3 7 5<\/pre>\n

Output:<\/strong><\/p>\n

The given matix after swapping the major and the minor diagonal elements: \r\n0 2 6 \r\n1 4 2 \r\n5 7 3<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Matrix : \r\n1 2 3\r\n4 5 6\r\n7 8 9<\/pre>\n

Output:<\/strong><\/p>\n

The given matix after swapping the major and the minor diagonal elements: \r\n3 2 1 \r\n4 5 6 \r\n9 8 7<\/pre>\n

Program to Swap Major and Minor Diagonals of a Square Matrix in Python:<\/h2>\n

Below are the ways to swap the major diagonal elements and minor diagonal elements of a given matrix.<\/p>\n