Skip to content
DeepForgeby svx
All problems
EasyOptimizationop-078

Hard Threshold Vector

Problem

Apply the elementwise hard-thresholding operator.

Keep each component unchanged when abs(x) > t, otherwise return 0.0. Components exactly at the threshold are zeroed. An empty list returns [].

Hint

Unlike soft thresholding, surviving values are not shrunk.

Test cases

case 1
in: [[1.5,-0.5,3,0.1],1]
expected: [1.5,0,3,0]
case 2
in: [[2,-2],1]
expected: [2,-2]
case 3
in: [[],0.5]
expected: []
case 4
in: [[1,-1],1]
expected: [0,0]
Starter codePython
def hard_threshold_vector(v, t):
    # Your code here
    pass

4 test cases · run your code against them in the editor