Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python minimum value of matrix

Tags:

python

matrix

Say I have a matrix like the following.

[[1,2]
 [3,4]]

I want the number 1 to be returned as the minimum.
Currently I have

a = np.array([[1,2],[3,4]])
min([min(element) for element in a])
>> 1

Is there a more efficient way of doing this?
I don't like my solution above. I tried min(a) which gives an error. I have read the answers provided in Find maximum and minimum value of a matrix and I feel that what I have is slightly better?

like image 493
AsheKetchum Avatar asked Mar 01 '17 15:03

AsheKetchum


1 Answers

Given your matrix

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])

You can just call the min method off your matrix

>>> a.min()
1

Or call the free function min and pass in your matrix

>>> np.min(a)
1
like image 167
Cory Kramer Avatar answered Oct 31 '22 16:10

Cory Kramer