Q:

Write a NumPy program to extract upper triangular part of a NumPy matrix

0

Write a NumPy program to extract upper triangular part of a NumPy matrix.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
num = np.arange(18)
arr1 = np.reshape(num, [6, 3])
print("Original array:")
print(arr1)
result  = arr1[np.triu_indices(3)]
print("\nExtract upper triangular part of the said array:")
print(result)
result  = arr1[np.triu_indices(2)]
print("\nExtract upper triangular part of the said array:")
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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now