MinMaxElem - min and max element of array(s)
Differences to Matlab's MIN and MAX:
- Both MIN and MAX are searched simultaneously for speed.
- The largest and smallest element are replied independent from the
dimensions. This is equivalent to MIN(X(:)), MAX(X(:)).
- Multiple inputs can be searched at once.
- Can exclude infinite values on the fly.
- Speed: Getting the min/max element of a vector:
[1x1E3] -> 2 times faster, [1x1E5] -> 5 times faster
SINGLE(RAND(1,1E5)) -> 7 times faster than [MIN(X), MAX(X)].
(MSVC 2008, SSE2 enabled, Matlab 2009a, single-core)
[Min, Max] = MinMaxElem(X)
[Min, Max] = MinMaxElem(X, Y, ...)
[Min, Max] = MinMaxElem(X, Y, ..., 'finite')
[Min, Max, MinIndex, MaxIndex, MinArg, MaxArg] = MinMaxElem(X, Y, ...)
INPUT:
X, Y, ...: Real arrays of type: DOUBLE, SINGLE, (U)INT8/16/32/64.
The sizes can differ.
Finite: Optional. For 'finite', infinite values are ignored.
OUTPUT:
Min, Max: Minimal and maximal elements of all input arrays.
MinIndex, MaxIndex: Linear index related to the array the values are found in.
MinArg, MaxArg: Number of the input argument the values are found in.
EXAMPLES:
t = 0:10000;
[minV, maxV] = MinMaxElem(sin(t))
% minV = -0.999993477, maxV = 0.9999935858
[minV, maxV, minI, maxI, minA, maxA] = MinMaxElem(sin(t), cos(t))
% minV = -0.9999999995, maxV = 1: Extremal value
% minI = 356, maxI = 1: Indices related to corresponding array
% minA = 2, maxA = 2: Min and Max found in 2nd argument
Find the cell which contains the Min/Max elements:
C = num2cell(rand(10, 10), 1);
[minV, maxV, minI, maxI, minA, maxA] = MinMaxElem(C{:})
% minA and maxA contain the corresponding cell index.
COMPILATION:
Calling the M-version once starts the compilation of the C-sources autmatically.
Run uTest_MinMaxElem to test validity and speed!
Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit
Jan (2021). MinMaxElem (https://www.mathworks.com/matlabcentral/fileexchange/30963-minmaxelem), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
@Jim: It would be useful for me to receive your unit-test output by email. I guess, that the /fp:fast flag impedes the detection of NaN's?
This tool was faster for Matlab 2009a. But modern Matlab versions apply a better MAX algorithm and profit from multi-threading. Therefore only the 'finite' flag is a real benefit of this submission now and I will remove the "speed claims" from the documentation, until I add multi-threading also.
Thanks for your helpful comment.
Hi Jan,
I'm using 64 bit 2014a with 4 processors (8 cores). Matlab uses multiple cores for min and max as seen in the Performance section of the Windows task manager. Also, with x64 SSE2 doesn't exist.
http://msdn.microsoft.com/en-us/library/vstudio/jj620901(v=vs.110).aspx
AVX provides a small speedup but sadly you really need multiple core code to compete.
Also, some of the unit tests fail now ... perhaps due to the fp:/fast flag.
Jim
I'd appreciate recieving the output of the speed measurements by email, especially for newer Matlab version, multi-core processors and 64 bit environments. Thanks.