1
A = numpy.array([[0,1,2], [3,4,5], [5,4,1]])

I want to compute median of each row and then zero out all values that are less than the median. How do I do that?

1 Answer 1

4
A = np.array([[0,1,2], [3,4,5], [5,4,1]])
medians=np.median(A,axis=1)[np.newaxis].T
A[A<medians]=0

A=
 [[0 1 2]
 [3 4 5]
 [5 4 1]]

Medians=
 [[ 1.]
 [ 4.]
 [ 4.]]

A after subtracting medians
 [[0 1 2]
 [0 4 5]
 [5 4 0]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.