﻿ NumPy: Get a copy of a matrix with the elements below the k-th diagonal zeroed - w3resource # NumPy: Get a copy of a matrix with the elements below the k-th diagonal zeroed

## NumPy: Array Object Exercise-154 with Solution

Write a NumPy program to get a copy of a matrix with the elements below the k-th diagonal zeroed.

Sample Solution:

Python Code:

``````import numpy as np
result  = np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
print("\nCopy of a matrix with the elements below the k-th diagonal zeroed:")
print(result)
```
```

Sample Output:

```Copy of a matrix with the elements below the k-th diagonal zeroed:
[[ 1  2  3]
[ 4  5  6]
[ 0  8  9]
[ 0  0 12]]
```

Python Code Editor:

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

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Set comprehension:

```>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}
```