필터 지우기
필터 지우기

max and min values in an array

조회 수: 56 (최근 30일)
Deep P
Deep P 2016년 12월 12일
댓글: dpb 2016년 12월 12일
Hello,
I am trying to find positive and negative maximum and minimum in an array.
Lets say I have a Matrix A=[1 -2 5;3 7 -9,4 6 8] if I use max and min functions on A,I get max(A)=[4 7 8] and min(A)=[1 -2 -9]. Since I wanted one singe number I performed max and min operations again.
max(max(A))= 8 and min(min(A))=-9
My requirement here is to get both positive and negative maximum and positive and negative minimum. Is there any way to find that?
Thank you.
  댓글 수: 1
the cyclist
the cyclist 2016년 12월 12일
Your language is not very clear to me. By "maximum" and "minimum", are you talking about the magnitude? So, is the following correct?
  • Maximum (magnitude) positive = 8
  • Minimum (magnitude) positive = 1
  • Maximum (magnitude) negative = -9
  • Minimum (magnitude) positive = -2

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

답변 (2개)

the cyclist
the cyclist 2016년 12월 12일
편집: the cyclist 2016년 12월 12일
If what I said in my comment is correct, then do the following:
maxMagPos = max(A(A>0))
minMagPos = min(A(A>0))
maxMagNeg = min(A(A<0))
minMagNeg = max(A(A<0))
  댓글 수: 3
the cyclist
the cyclist 2016년 12월 12일
The best form of thanks is upvoting and/or accepting answers. This rewards the contributor, and points future users to good answers.
I suggest you open a new post for your second question, which is quite different in nature, not really related to this one.
dpb
dpb 2016년 12월 12일
@OP If this is something you do more than once, then I'd suggest writing a little utility function that returns the values in a single call; either the four variables or an array depending on how your application most frequently would use the results.

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


dpb
dpb 2016년 12월 12일
It's the colon operator that's your friend here...
mnx=min(x(:)); % doc colon for details of the "magic" here...

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by