{"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 Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to interchange the elements of the first and the last rows of a given matrix in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program for Squares of Matrix Diagonal Elements Given a matrix, the task is to interchange the elements of the first and the last rows of a given matrix. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix …<\/p>\nGiven Matrix : \r\n2 3 4\r\n1 7 45\r\n8 2 3<\/pre>\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
Given Matrix : \r\n1 2 3 \r\n4 5 6 \r\n7 8 9<\/pre>\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
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the matrix as static input and store it in a variable.\r\nmtrx = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\r\n# Calculate the number of rows of the given matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(mtrx[0])\r\nprint(\"The given matix after swapping the first and last row elements: \")\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the For loop, swap mtrx[0][itr], mtrx[mtrxrows-1][itr] using the comma(,) operator\r\n # ( where itr is the iterator value and mtrxrows is the no of rows of matrix).\r\n\r\n mtrx[0][itr], mtrx[mtrxrows-1][itr] = mtrx[mtrxrows-1][itr], mtrx[0][itr]\r\n\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Print the element of the matrix by printing gvnmatrix[n][m] value\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n print(mtrx[n][m], end=' ')\r\n print()\r\n<\/pre>\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
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n\r\n mtrx.append(l)\r\n\r\nprint(\"The given matix after swapping the first and last row elements: \")\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the For loop, swap mtrx[0][itr], mtrx[mtrxrows-1][itr] using the comma(,) operator\r\n # ( where itr is the iterator value and mtrxrows is the no of rows of matrix).\r\n\r\n mtrx[0][itr], mtrx[mtrxrows-1][itr] = mtrx[mtrxrows-1][itr], mtrx[0][itr]\r\n\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Print the element of the matrix by printing gvnmatrix[n][m] value\r\n # where n is the iterator value of the parent For loop and m is the iterator\r\n # value of the inner For loop.\r\n print(mtrx[n][m], end=' ')\r\n print()<\/pre>\n
Enter some random number of rows of the matrix = 3\r\nEnter some random number of columns of the matrix = 3\r\nEnter {3} elements of row {1} separated by spaces = 2 3 4\r\nEnter {3} elements of row {2} separated by spaces = 1 7 45\r\nEnter {3} elements of row {3} separated by spaces = 8 2 3\r\nThe given matix after swapping the first and last row elements: \r\n8 2 3 \r\n1 7 45 \r\n2 3 4<\/pre>\n
\n