Python Program to Display Upper Triangular Matrix


A matrix is a two-dimensional array of many numbers arranged in rows and columns. A square matrix (whose rows and columns has same number of elements) has two diagonals. One is the Primary diagonal - located from the top left corner to the bottom right corner of a square matrix. And the second one is the Secondary diagonal - located from the top right to the bottom left corner.

For a square matrix if all the elements below the Primary diagonal are zero then it is called the Upper Triangular Matrix.

[1, 3, 4]
[0, 5, 6]
[0, 0, 3]

A matrix cannot be converted to the upper triangular matrix if the given matrix is not a square matrix.

Input Output Scenarios

Assume we have a square matrix. And output matrix will be the upper triangular matrix.

Input matrix:
[1, 3, 5, 7]
[9, 2, 4, 2]
[6, 3, 1, 4]
[5, 8, 7, 6]
 
Upper triangular matrix:
[1, 3, 5, 7]
[0, 2, 4, 2]
[0, 0, 1, 4]
[0, 0, 0, 6]

Let's see the below examples to display the upper triangular matrix. We will use python list of lists to create matrix.

Example

In this example we will displayed the upper triangle matrix by replacing lower triangle elements (4, 8, 1) to zero’s.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
if(len(arr) != len(arr[0])):  
   print("Matrix should be a square matrix");  
else: 
   for i in range(3):
      for j in range(3):
         if(i<=j):
            print(arr[i][j],end="  ")
         else:
            print(0,end="  ")
      print()

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1  2  3  
0  5  6  
0  0  5 

Example

In this example we only displayed the upper triangle matrix. Instead of converting lower trying elements to zero’s.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
for i in range(3):
   for j in range(3):
      if(i > j):
         print(end="  ")
      else:
         print(arr[i][j],end=" ")
   print(" ")

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1 2 3  
  5 6  
    5  

Example

In this example we will update the original matrix by converting the lower triangle elements to zero, then we will display the upper triangle matrix.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
for i in range(3):
   for j in range(3):
      if(i > j):
         arr[i][j] = 0
         print(arr[i][j],end=" ")
      else:
         print(arr[i][j],end=" ")
   print(" ")

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1 2 3  
0 5 6  
0 0 5  

Updated on: 15-May-2023

838 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements