# Extract the lower triangular part of a 4x4 Random array using NumPy

## NumPy: Advanced Exercise-22 with Solution

Write a NumPy program to create a 4x4 array with random values and extract the lower triangular part of the matrix.

This NumPy program creates a 4x4 array filled with random values and then extracts its lower triangular portion. The lower triangular part comprises the elements below the main diagonal.

**Sample Solution:**

**Python Code:**

```
# Importing the necessary NumPy library
import numpy as np
# Create a 4x4 array with random values
array = np.random.rand(4, 4)
# Extract the lower triangular part of the matrix
lower_triangular = np.tril(array)
# Printing the original array and its lower triangular part
print("4x4 Array:\n", array)
print("Lower Triangular Part of the Array:\n", lower_triangular)
```

Output:

4x4 Array: [[0.24206431 0.85784058 0.37914539 0.36559226] [0.66368087 0.43859816 0.18437344 0.20330004] [0.46842665 0.25517043 0.04631836 0.43460557] [0.56584682 0.64265237 0.91558094 0.75886061]] Lower Triangular Part of the Array: [[0.24206431 0. 0. 0. ] [0.66368087 0.43859816 0. 0. ] [0.46842665 0.25517043 0.04631836 0. ] [0.56584682 0.64265237 0.91558094 0.75886061]]

**Explanation:**

- Import NumPy library: This step imports the NumPy library, which is essential for numerical operations.
- Create a 4x4 array: We use np.random.rand(4, 4) to generate a 4x4 matrix with random values between 0 and 1.
- Extract the lower triangular part of the matrix: The np.tril function extracts the lower triangular part of the matrix, setting elements above the main diagonal to zero.
- Print results: This step prints the original 4x4 array and its lower triangular part.

**Python-Numpy Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Extract the uper triangular part of a 4x4 random array using NumPy.

**Next:** Generate 6x6 array and compute Matrix rank with NumPy.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics