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
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)
Try replacing with
C = np.minimum.reduce([A, B])
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
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)
Try replacing with
C = np.minimum.reduce([A, B])