In the previous article, we have discussed Python Program to Find the Sum of all Diagonal Elements of a Matrix
Given a matrix and the task is to display the lower triangular matrix of the given matrix in Python.
What is a matrix:
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.
Example:
Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.
In this order, the dimensions of a matrix indicate the number of rows and columns.
Here as there are 5 rows and 4 columns it is called a 5*4 matrix.
Lower Triangular Matrix:
A lower triangular matrix is one that has all of its upper triangular elements equal to zero. In other words, all non-zero elements are on the main diagonal or in the lower triangle.
Examples:
Example1:
Input:
Given Matrix : 5 3 2 6 1 5 4 8 2
Output:
The Lower Triangular matrix of the given matrix is : 5 0 0 6 1 0 4 8 2
Example2:
Input:
Given Matrix : 1 2 3 6 5 4 7 8 9
Output:
The Lower Triangular matrix of the given matrix is : 1 0 0 6 5 0 7 8 9
Program to Display a Lower Triangular Matrix in Python
Below are the ways to display the lower triangular matrix of the given matrix in Python:
Method #1: Using For Loop (Static Input)
Approach:
- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the condition n is less than m using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- If the statement is true, then print 0.
- Else Print the element of the matrix by printing gvnmatrix[n][m] value.
- The Exit of the Program.
Below is the implementation:
# Give the matrix as static input and store it in a variable. mtrx = [[5, 3, 2], [6, 1, 5], [4, 8, 2]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) print("The Lower Triangular matrix of the given matrix is :") # To print all the elements of the given matrix. # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if the condition n is less than m using the if conditional statement where n # is the iterator value of the parent For loop and m is the iterator value of the # inner For loop. if n < m: # If the statement is true, then print 0. print("0 ", end="") else: # Else Print the element of the matrix by printing gvnmatrix[n][m] value. print(mtrx[n][m], end=" ") print()
Output:
The Lower Triangular matrix of the given matrix is : 5 0 0 6 1 0 4 8 2
Method #2: Using For loop (User Input)
Approach:
- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
- Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to gvnmatrix using the append() function.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the condition n is less than m using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
- If the statement is true, then print 0.
- Else Print the element of the matrix by printing gvnmatrix[n][m] value.
- The Exit of the Program.
Below is the implementation:
# Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) print("The Lower Triangular matrix of the given matrix is :") # To print all the elements of the given matrix. # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if the condition n is less than m using the if conditional statement where n # is the iterator value of the parent For loop and m is the iterator value of the # inner For loop. if n < m: # If the statement is true, then print 0. print("0 ", end="") else: # Else Print the element of the matrix by printing gvnmatrix[n][m] value. print(mtrx[n][m], end=" ") print()
Output:
Enter some random number of rows of the matrix = 3 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 1 2 3 Enter {3} elements of row {2} separated by spaces = 6 5 4 Enter {3} elements of row {3} separated by spaces = 7 8 9 The Lower Triangular matrix of the given matrix is : 1 0 0 6 5 0 7 8 9
Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.