Find Min& Max Togather
27 views (last 30 days)
Jan on 13 Jun 2012
A C-mex, which checks for min and max simulataneously: FEX: MinMaxElem . NaN's are ignored and if wanted +/-Inf also. This is much more efficient than "max(x(isfinite(x))". In addition the total min/max over multiple arrays can be found.
This function is much faster when compiled by MSVC under Windows compared to MIN and MAX of Matöab 2009a. But a user has reported, that it is slower than Matlab 2012a when compiled by GCC under Linux. I assume, that MSVC can convert the code to SSE.
Eric on 20 May 2020
As of 2017a (and for anyone else still stumbling across this question years later, which it looks like there's still a handful), MATLAB has a built-in function called bounds(), which is basically Geoff's answer, but with all the fancy MATLAB argument syntax included as well. Note that this will not get you the indices for these values. If you want that, you'll still need to call min and max directly. Or just write your own function.
Geoff on 13 Jun 2012
Well, I do it like this:
function [Amin, Amax] = minmax( A )
Amin = min(A);
Amax = max(A);
Note: Not to be confused with minimax algorithm!
You could beef it up if necessary to handle the different inputs and outputs of min and max.
But this may be all you need. I admit it is using min and max separately, but your calling code doesn't care. I don't know of a native function that does this. It is only marginally less efficient than any code that could be written to find both at once.