NumPy: Extract upper triangular part of a NumPy matrix
NumPy: Array Object Exercise-153 with Solution
Extract upper triangular part of a matrix.
Write a NumPy program to extract the upper triangular part of a NumPy matrix.
Sample Solution:
Python Code:
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
# Generating a NumPy array 'num' containing numbers from 0 to 17 using arange()
num = np.arange(18)
# Reshaping the array 'num' into a 6x3 matrix and assigning it to 'arr1'
arr1 = np.reshape(num, [6, 3])
# Displaying a message indicating the original array will be printed
print("Original array:")
# Printing the original array 'arr1'
print(arr1)
# Extracting the elements of the upper triangular part of the 3x3 square array 'arr1'
result = arr1[np.triu_indices(3)]
# Displaying a message indicating the upper triangular part of the array will be printed
print("\nExtract upper triangular part of the said array:")
# Printing the extracted upper triangular part of the array 'arr1'
print(result)
# Extracting the elements of the upper triangular part of the 2x2 square array 'arr1'
result = arr1[np.triu_indices(2)]
# Displaying a message indicating the upper triangular part of the array will be printed
print("\nExtract upper triangular part of the said array:")
# Printing the extracted upper triangular part of the array 'arr1'
print(result)
Sample Output:
Original array: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12 13 14] [15 16 17]] Extract upper triangular part of the said array: [0 1 2 4 5 8] Extract upper triangular part of the said array: [0 1 4]
Explanation:
In the above code –
num = np.arange(18): This line creates a 1D NumPy array called ‘num’ with elements from 0 to 17.
arr1 = np.reshape(num, [6, 3]): This line reshapes the num array into a 2D array ‘arr1’ with 6 rows and 3 columns.
result = arr1[np.triu_indices(3)]: This line uses np.triu_indices(3) to generate the indices of the upper triangle of a 3x3 array. These indices are then used to index into ‘arr1’. Since ‘arr1’ has only 3 columns, the result will contain all elements in the first three rows and three columns of ‘arr1’, which form an upper triangular matrix.
result = arr1[np.triu_indices(2)]: This line overwrites the previous result by using np.triu_indices(2) to generate the indices of the upper triangle of a 2x2 array. These indices are then used to index into ‘arr1’. In this case, the result will contain only the elements in the first two rows and two columns of arr1, which form a 2x2 upper triangular matrix.
Finally print() function prints the upper triangular part of the said array.
Pictorial Presentation:
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to calculate the sum of all columns of a 2D NumPy array.
Next: Write a NumPy program to get a copy of a matrix with the elements below the k-th diagonal zeroed.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-153.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics