{"id":20806,"date":"2021-09-21T09:00:32","date_gmt":"2021-09-21T03:30:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=20806"},"modified":"2021-11-22T18:36:15","modified_gmt":"2021-11-22T13:06:15","slug":"python-program-to-interchange-elements-of-first-and-last-rows-in-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-interchange-elements-of-first-and-last-rows-in-matrix\/","title":{"rendered":"Python Program to Interchange Elements of First and Last Rows in Matrix"},"content":{"rendered":"

In the previous article, we have discussed Python Program for Squares of Matrix Diagonal Elements<\/a><\/p>\n

Given a matrix, the task is to interchange the elements of the first and the last rows 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

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.<\/div>\n

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

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

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

Given Matrix : \r\n2 3 4\r\n1 7 45\r\n8 2 3<\/pre>\n

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

The given matix after swapping the first and last row elements: \r\n8 2 3 \r\n1 7 45 \r\n2 3 4<\/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 first and last row elements: \r\n7 8 9 \r\n4 5 6 \r\n1 2 3<\/pre>\n

Program to Interchange Elements of First and Last Rows in Matrix in Python<\/h2>\n

Below are the ways to interchange the elements of the first and the last rows of a given matrix in python:<\/p>\n