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

79 views (last 30 days)
Faith McColl on 7 Jul 2015
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 CommentShowHide None
Siranjeevi Gurumani on 17 Sep 2021
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 on 7 Jul 2015
x = [10 20 30 40 50 50];
x(find(x == max(x), 1, 'first'))
or simply
max(x)
##### 1 CommentShowHide None
Faith McColl on 7 Jul 2015
Thank you for your help. I must be doing something wrong elsewhere in the equation.

### More Answers (2)

Sean de Wolski on 7 Jul 2015
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 CommentsShowHide 1 older comment
Sean de Wolski on 7 Jul 2015
mx = max([-5, -6, -8, -7, 1, 6, 6, 5, -3, -4, -6, -9])
I think you're over-thinking it :)

Siranjeevi Gurumani on 17 Sep 2021
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