Find Min& Max Togather

15 ビュー (過去 30 日間)
Tallha Akram
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
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
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
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 件のコメント
Eric
Eric 2020 年 5 月 20 日
I'm a little surprised that after all this time, the best MATLAB has on this front is to just to throw these two functions (min/max) together in a wrapper. Maybe it really is just as fast to call each function separately, but I find it quite hard to believe that going through a list of values twice to find a value is just as fast as going through the list once and finding both values simultaneously...
Walter Roberson
Walter Roberson 2020 年 5 月 20 日

サインインしてコメントする。


Ken Atwell
Ken Atwell 2012 年 6 月 13 日
Interesting coincidence: Loren Shure blogged about combining min and max some four years ago.

Geoff
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 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 13 日
A combined min-max can be about twice as efficient, as it is only necessary to load each array element from memory once in order to do the comparisons for min and max. Probably a page of memory at a time would be loaded into secondary cache, and probably 16 to 128 bytes "nearby" would be loaded into primary cache. Then the single load into a register (and then to compare the CPU register to the running min and max values that are already in registers) would be faster than two references to primary cache, and primary cache is usually 8-10 times faster than secondary cache, and secondary cache is usually about 10 times faster than main memory.
If you have to run through the entire array again in order to do the operation you did not do, you _might_ get lucky and have everything all in primary cache (for a very small arrays) or in secondary cache (for moderate arrays), but otherwise you end up having to load the entire array from main memory, somewhere maybe 500 times slower than register computations.
There are a lot of hidden costs to running through the same array twice compared to only running through it once with a slight increase of complexity of what is done with each element. The increased complexity operates at full CPU speed; main memory is a bottle-neck.
Geoff
Geoff 2012 年 6 月 13 日
Hmmm good point. So I guess the best answer is: write it in C. =)

サインインしてコメントする。


Teja Muppirala
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 件のコメント
Teja Muppirala
Teja Muppirala 2012 年 6 月 14 日
http://www.mathworks.com/help/toolbox/nnet/ref/minmax.html
Teja Muppirala
Teja Muppirala 2012 年 6 月 14 日
(I think it's easy enough to just write min and max though...)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by