image thumbnail

MinMaxElem

version 1.0.0.0 (14 KB) by Jan
Min and max element and index - C-Mex faster than MIN/MAX

611 Downloads

Updated 04 Apr 2011

View License

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

Cite As

Jan (2021). MinMaxElem (https://www.mathworks.com/matlabcentral/fileexchange/30963-minmaxelem), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!