MATLAB Answers

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

4 views (last 30 days)
jaah navi
jaah navi on 17 Oct 2019
Commented: Jos (10584) on 17 Oct 2019
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 Comment

dpb
dpb on 17 Oct 2019
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.

Sign in to comment.

Answers (3)


Jos (10584)
Jos (10584) on 17 Oct 2019
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:

  0 Comments

Sign in to comment.


Daniel M
Daniel M on 17 Oct 2019
Edited: Daniel M on 17 Oct 2019
A = [23 42 37 18 52];
minmaxVals = prctile(A,[0 100])
ans =
18 52

  3 Comments

jaah navi
jaah navi on 17 Oct 2019
ok.But when i tried with respect to the following array
A=[1.0992 1.5107 1.6426
2.6664 2.7159 2.7833
0.7849 0.4077 0.3900
1.0745 1.0196 1.0966
0.5537 0.6946 0.8196
1.6301 1.6503 1.7238]
minmaxVals = prctile(A,[0 100])
it gives the following output
minmaxVals =[ 0.0752 0.1222;
2.8012 1.6739]
I want to make these values to run into the following code;
C=zeros(6,1);
[value, index] = min(A,[],2) % [value, index] = minmaxVals(A,[],2)
V=value;
I=index;
C(:,1) = index
Could you please help me on this.
Walter Roberson
Walter Roberson on 17 Oct 2019
As I have been telling you: use the simple ways first and get them working, and do not worry about fancy ways until you have significant performance problems.
So just call min() and max() instead of spending a lot of time looking for a function that will return the values and indices. Or write a small function yourself that uses min and max internally to return what you want.

Sign in to comment.

Sign in to answer this question.

Tags