# Find Min& Max Togather

조회 수: 101(최근 30일)
Tallha Akram 2012년 6월 13일
댓글: Walter Roberson 2020년 5월 20일
Hi,
How to find Minimum and Maximum of array with a single command. I dont want to use min and max separately .
Regards
Tallha Akram

댓글을 달려면 로그인하십시오.

### 답변(5개)

Jan 2012년 6월 13일
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.
##### 댓글 수: 1표시숨기기 없음
Jan 2012년 6월 14일
Since Matlab 2011b (or a?) MIN and MAX are multi-threaded for about > 10'000 elements. The published MinMaxElem uses one core only. This has surprising effects: The C-mex is faster than Matlab's Min&Max for large DOUBLE vectors and small SINGLE vectors. The included unit-test function can be used for speed comparisons.

댓글을 달려면 로그인하십시오.

Eric 2020년 5월 20일
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.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Walter Roberson 2020년 5월 20일
It looks like Intel's MKL has a way of computing both at the same time, for at least some kinds of arrays.

댓글을 달려면 로그인하십시오.

Ken Atwell 2012년 6월 13일
Interesting coincidence: Loren Shure blogged about combining min and max some four years ago.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Geoff 2012년 6월 13일
Well, I do it like this:
function [Amin, Amax] = minmax( A )
Amin = min(A);
Amax = max(A);
end
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.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Geoff 2012년 6월 13일
Hmmm good point. So I guess the best answer is: write it in C. =)

댓글을 달려면 로그인하십시오.

Teja Muppirala 2012년 6월 14일
The Neural Network Toolbox actually already has a function to do this called MINMAX. It calculates the min and max of each row of a matrix.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Teja Muppirala 2012년 6월 14일
(I think it's easy enough to just write min and max though...)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by