Could anyone suggest me is there any way of choosing maximum and minimum values together

2 ビュー (過去 30 日間)
A = [23 42 37 18 52];
With respect to command M = max(A) gives 52
min(A) gives 18
Is there any way so that i can get both 52 and 18 together(maximum and minimum together)
  1 件のコメント
dpb
dpb 2019 年 10 月 17 日
mnmx=[min(A) max(A)];
or write your own "syntactic sugar" routine minmax()--
function mnmx=minmax(x)
mnmx=[min(x(:)) max(x(:))];
end
The latter should probably also have the ability to return the locations to mimic builtin interfaces.....and maybe treat arrays the same as them also in working on column-basis by default with the dimension as the optional argument...so many choices to make.

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

回答 (3 件)

Walter Roberson
Walter Roberson 2019 年 10 月 17 日

Jos (10584)
Jos (10584) 2019 年 10 月 17 日
Here is a nice trick that also allows you to combine functions in a single call, which also can return the other outputs of these functions:
minmaxFun = @(x) cellfun(@(F) F(x), {@min, @max}) ;
[V, I] = minmaxFun([2 1 3 5 2])
% V = [1 5], I = [2 4]
You can take this a step further:
mapF = @(val, fcns) cellfun(@(f) f(val{:}), fcns) ;
data = randi(10, [1 10])
[minmax, minmaxIDX] = mapF({data}, {@min @max})
S = mapF({data}, {@mean @median @std})
You can learn much more about this at Loren's Blog:

Daniel M
Daniel M 2019 年 10 月 17 日
編集済み: Daniel M 2019 年 10 月 17 日
A = [23 42 37 18 52];
minmaxVals = prctile(A,[0 100])
ans =
18 52
  3 件のコメント
Jos (10584)
Jos (10584) 2019 年 10 月 17 日
I second that, Walter! +1

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by