How to calculate element wise minimum/maximum between two same size numpy matrices in Python?

Question:

I tried this way:

A = np.asmatrix(np.random.rand(4,1))
B = np.asmatrix(np.random.rand(4,1))
C = np.min(A, B)

Let’s A and B be as given below:

A = [[0.13456968]
     [0.80465702]
     [0.08426155]
     [0.85041178]]

B = [[0.64932459]
     [0.77806739]
     [0.15517366]
     [0.10992883]]

I want to have C as given below:

C = [[0.13456968]
     [0.77806739]
     [0.08426155]
     [0.10992883]]

But this gives following error:

TypeError: only integer scalar arrays can be converted to a scalar index
Asked By: Masum Bhuiyan

||

Answers:

You need to use minimum() instead of min():

A = np.asmatrix(np.random.rand(4,1))
B = np.asmatrix(np.random.rand(4,1))
C = np.minimum(A, B)
Answered By: alexey_zotov

Try replacing with

C = np.minimum.reduce([A, B])
Answered By: esantix
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.