Home > Backend Development > Python Tutorial > Python program to print matrix boundary elements

Python program to print matrix boundary elements

WBOY
Release: 2023-08-26 14:01:06
forward
680 people have browsed it

Python program to print matrix boundary elements

Boundary Elements of a Matrix

Elements that are not surrounded by other elements belonging to the same matrix are called boundary elements. Taking advantage of this phenomenon, we can build a program. Let's consider an input-output scenario and build a program.

Input and output scenarios

Consider a matrix (square matrix)

  • The Boundary elements are the elements except the middle elements of the matrix.

  • The middle element of the matrix is ​​5, and there is no other middle element except 5.

  • So, the Boundary elements are 9, 8, 7, 6, 4, 3, 2, and 1 as they are lying in the boundary positions of the matrix.

9  8  7
6  5  4
3  2  1
Copy after login

algorithm

  • Step 1 − Starting from the initial element of the matrix, traverse the elements of the array, which represents a matrix.

  • Step 2 − We iterate over the elements of the matrix using a two-dimensional array, where one dimension represents the rows and the other dimension represents the columns. Therefore, the outer loop represents the rows of the matrix and the inner loop represents the columns of the matrix.

  • Step 3 - If the element belongs to the first or last row or the first or last column, then the element can be considered as a border element and can be printed.

  • Step 4 - If not, the element must be considered a non-boundary element and should be skipped. In this case, a space should be printed in place of the non-boundary element.

Example

In the following example, we are going to discuss about the process of finding the boundary elements in a matrix.

def functionToPrint(arra, r, c):
   for i in range(r):
      for j in range(c):
         if (i == 0):
            print(arra[i][j])
         elif (i == r-1):
            print(arra[i][j]) 
         elif (j == 0):
            print(arra[i][j])
         elif (j == c-1):
            print(arra[i][j])
         else:
            print(" ")

if __name__ == "__main__":
   arra = [[1, 2, 3, 4], [5, 6, 7, 8],
      [9, 10, 11, 12], [13, 14, 15, 16]]

   print("The boundary elements of the given matrix are: ")
   functionToPrint(arra, 4, 4)
Copy after login

Output

The output of the above program is as follows:

The boundary elements of the given matrix are: 
1
2
3
4
5


8
9


12
13
14
15
16
Copy after login

The above is the detailed content of Python program to print matrix boundary elements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template