How to return only one of multiple identical max/min values?

20 ビュー (過去 30 日間)
Faith McColl
Faith McColl 2015 年 7 月 7 日
回答済み: Siranjeevi Gurumani 2021 年 9 月 17 日
I have multiple series, and in each one I want to find the maximum or minimum value and store it into a variable so at the end I have a matrix of maximums and a matrix of minimums. The problem is that some of the series appear to have more than one maximum (or minimum) and the program gives me an error message. How do I make Matlab return only the first of these values?
  1 件のコメント
Siranjeevi Gurumani
Siranjeevi Gurumani 2021 年 9 月 17 日
Assuming A = matrix of more than one dimensions
and you want max of whole matrix
A = [1,6,6;4,5,6];
max(max(A))
ans = 6
or
max(A,[],'all')
ans = 6

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

採用された回答

Thorsten
Thorsten 2015 年 7 月 7 日
x = [10 20 30 40 50 50];
x(find(x == max(x), 1, 'first'))
or simply
max(x)
  1 件のコメント
Faith McColl
Faith McColl 2015 年 7 月 7 日
Thank you for your help. I must be doing something wrong elsewhere in the equation.

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

その他の回答 (2 件)

Sean de Wolski
Sean de Wolski 2015 年 7 月 7 日
This is what the max and min function both do. Can you provide a minimal working example of what you have, what you want, and what you're doing now?
  2 件のコメント
Faith McColl
Faith McColl 2015 年 7 月 7 日
I have a series that is something like [-5, -6, -8, -7, 1, 6, 6, 5, -3, -4, -6, -9]. I already have Matlab programmed to separate the negative series from the positive series, and to find the maximum of each positive series and the minimum of each negative series. It gives me an error message because there are two maximums in the positives and it cannot store it as one value in the matrix. What I'm wondering is how to make Matlab return simply "6" (in this case) for the maximum of the positive series rather than an error message because there are two.
Sean de Wolski
Sean de Wolski 2015 年 7 月 7 日
mx = max([-5, -6, -8, -7, 1, 6, 6, 5, -3, -4, -6, -9])
I think you're over-thinking it :)

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


Siranjeevi Gurumani
Siranjeevi Gurumani 2021 年 9 月 17 日
Assuming A = matrix of more than one dimensions
and you want max of whole matrix
A = [1,6,6;4,5,6];
max(max(A))
ans = 6
or
max(A,[],'all')
ans = 6

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by