{"id":19486,"date":"2021-08-30T16:06:16","date_gmt":"2021-08-30T10:36:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=19486"},"modified":"2021-11-22T18:37:07","modified_gmt":"2021-11-22T13:07:07","slug":"python-program-to-find-the-position-of-an-element-in-a-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-position-of-an-element-in-a-matrix\/","title":{"rendered":"Python Program to Find the Position of an Element in a Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Maximum Element in the Matrix<\/a> If the element does not exist in the given matrix then print the given element doesn’t exist in the given matrix.<\/p>\n If there are multiple answers we print all of them.<\/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.<\/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 Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the position of the given element in the given matrix in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Here element 9 is present in two positions so we print two of them.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Output2:(For Reference,<\/span> if the element doesn’t exist in the matrix)<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Find the Maximum Element in the Matrix Given a matrix and a number(element) the task is to find the position of the given element in the given matrix in Python. If the element does not exist in the given matrix then print the given element …<\/p>\n
\nGiven a matrix and a number(element) the task is to find the position of the given element in the given matrix in Python.<\/p>\nGiven Matrix :\r\n2\u00a0 \u00a09\u00a0 \u00a01\r\n11 4\u00a0 \u00a05 \r\n9\u00a0 \u00a02\u00a0 \u00a03\r\n1\u00a0 \u00a02\u00a0 \u00a03\r\nGiven Element = 9<\/pre>\n
The given element { 9 } is present at row {1} and column {2}\r\nThe given element { 9 } is present at row {3} and column {1}<\/pre>\n
Given Matrix :\r\n1 2\r\n3 4\r\nGiven Element =5<\/pre>\n
The given element doesnot exist in the given matrix.<\/pre>\n
Given Matrix :\r\n2\u00a0 \u00a05 6\r\n1 2 3\r\nGiven Element = 5<\/pre>\n
The given element { 5 } is present at row {1} and column {2}<\/pre>\n
Program to Find the Position of an Element in a 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 = [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]]\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\n# Give the number\/element as static input and store it in a variable to say gvnele.\r\ngvnele = 9\r\n# Take a variable to say tempo and initialize its value to 0\r\n# (Here tempo as temp variable which says whether the element is present\r\n# or not in matrix at the end).\r\ntempo = 0\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 # Check if the\u00a0gvnmatrix[n][m] value\r\n # (where n is the iterator value of the parent For loop and\r\n # m is the iterator value of the inner For loop)\r\n # is equal to gvnele using the if conditional statement.\r\n if(mtrx[n][m] == gvnele):\r\n # If it is true then print the iterator value of parent for loop+1\r\n # (Here it acts as row number)\r\n # and also print the inner loop iterator value+1\r\n # (Here it acts as column number).\r\n print('The given element {', gvnele, '} is present at row {' +\r\n str(n+1)+'} and column {'+str(m+1)+'}')\r\n # Set the value of tempo to 1.\r\n tempo = 1\r\n# After the end of loops check if the value of tempo is 0\r\n# or not using the if conditional statement.\r\nif(tempo == 0):\r\n # If it is true then print the given element doesn't exist in the given matrix.\r\n print('the given element doesnot exist in the given matrix.')\r\n<\/pre>\n
The given element { 9 } is present at row {1} and column {2}\r\nThe given element { 9 } is present at row {3} and column {1}<\/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# Give the number\/element as user input using the int(input()) function\r\n# and store it in a variable to say gvnele.\r\ngvnele = int(input('Enter some random element = '))\r\n# Take a variable to say tempo and initialize its value to 0\r\n# (Here tempo as temp variable which says whether the element is present\r\n# or not in matrix at the end).\r\ntempo = 0\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 # Check if the\u00a0gvnmatrix[n][m] value\r\n # (where n is the iterator value of the parent For loop and\r\n # m is the iterator value of the inner For loop)\r\n # is equal to gvnele using the if conditional statement.\r\n if(mtrx[n][m] == gvnele):\r\n # If it is true then print the iterator value of parent for loop+1\r\n # (Here it acts as row number)\r\n # and also print the inner loop iterator value+1\r\n # (Here it acts as column number).\r\n print('The given element {', gvnele, '} is present at row {' +\r\n str(n+1)+'} and column {'+str(m+1)+'}')\r\n # Set the value of tempo to 1.\r\n tempo = 1\r\n# After the end of loops check if the value of tempo is 0\r\n# or not using the if conditional statement.\r\nif(tempo == 0):\r\n # If it is true then print the given element doesn't exist in the given matrix.\r\n print('the given element doesnot exist in the given matrix.')\r\n<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 3\r\nEnter {3} elements of row {1} separated by spaces = 2 5 6\r\nEnter {3} elements of row {2} separated by spaces = 1 2 3\r\nEnter some random element = 5\r\nThe given element { 5 } is present at row {1} and column {2}<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 2\r\nEnter {2} elements of row {1} separated by spaces = 1 2\r\nEnter {2} elements of row {2} separated by spaces = 3 4\r\nEnter some random element = 5\r\nthe given element doesnot exist in the given matrix\r\n\r\n<\/pre>\n
\n