Code covered by the BSD License

### Highlights fromMIN2, MAX2

4.28571

4.3 | 7 ratings Rate this file 12 Downloads (last 30 days) File Size: 3.69 KB File ID: #22995

# MIN2, MAX2

16 Feb 2009 (Updated 23 Mar 2010)

Global min or max value of a 2-d array, the search may be limited to specified rows or columns

File Information
Description

It is often the case where one must find the overall minimum or maximum element of an array, along with the location of that element. This is a flaw of both find and min (or max), that they only work in one dimension.

Most solutions will have you convert the array to a vector, then find the min/max element, and convert the linear index back to row and column subscripts. While this does work on most problems, it will fail for REALLY huge arrays where the linear index is simply too large to fit into a 32 bit integer. The acknowledged file is one such example of a solution that internally converts the matrix to a vector for processing.

MIN2 and MAX2 do not do the conversion to a single linear index, so they have no problems here.

M = magic(4)
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

[minel,IJ] = min2(M)

minel =
1

IJ =
4 4

[maxel,IJ] = max2(M)

maxel =
16

IJ =
1 1

MIN2 and MAX2 also allow the user to restrict the search space, so if you wish to find the minimum over some subset of the rows and columns, but not look in the other rows and columns, we can do that too.

[minel,IJ] = min2(M,[],[1 2 3])

minel =
2

IJ =
1 2

[maxel,IJ] = max2(M,[2 3 4],[1 2 3])

maxel =
15

IJ =
4 3

When the minimal value is not unique, MIN2 and MAX2 will still find the same solution that find would have found, for consistency. MIN2 and MAX2 will also properly handle arrays that have inf or -inf elements in them, and ignore NaN elements in the search.

M2 = M + M';
M2(2,3) = inf;
M2(2,4) = nan
M2 =
32 7 12 17
7 22 Inf NaN
12 17 12 27
17 22 27 2

[minel,IJ] = min2(M2,1:3,1:3)

minel =
7
IJ =
2 1

[maxel,IJ] = max2(M2)

maxel =
Inf

IJ =
2 3

Acknowledgements

Maximum Or Minimum For N Dimension Array inspired this file.

MATLAB release MATLAB 7.5 (R2007b)
Other requirements Virtually any MATLAB release will use this code
Tags for This File
Everyone's Tags
Tags I've Applied
22 Mar 2010

Nicely written and documented functions. I will probably use them more often than the Matlab min and max.

One minor nit -- the example for max2 in the header gives the results for the min2 example.

19 Mar 2010
16 Mar 2009

Eliminates alot of case by case custom code I've been having to write. Thanks
CC

16 Mar 2009

I didnt findd it very useful or interesting at all! rank1!

13 Mar 2009
13 Mar 2009

Kevin has obviously decided to start targeting my work. See that he has made only three comments on any files, at all. Two of of those comments were inappropriately negative comments against my work. I wonder how I've hurt his feelings?

As far as min2 and max2 go, these functions supply utility that does not exist in matlab, as Kevin must know. They allow you to restrict the search space, while returning the location of the global minimum in one step.

13 Mar 2009

The max of the max and the min of the min? That's all?

18 Feb 2009

This is Pretty cool John. I really like the feature of limiting the search space.
Tks for it.
Regards, Marcelo.

17 Feb 2009

enjoy a nicely festooned min/max party in JD's matlab-kitchen with distinguished - thoughtful and verbose - help lines and comments as well as a meticulous (simple) computational engine...
will get 5 stars once turned into MINN and MAXN...
us