Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get second max element in matlab

Tags:

matlab

I have an array, say A = [ 3 5 6 7 ]. I know I can get the maximum value of this array with max(A) and it returns 7, but how can I get the second max (6) from this array without sorting or removing the first maximum value?

like image 407
Yuseferi Avatar asked Dec 03 '22 01:12

Yuseferi


1 Answers

I can propose following tricky solution:

second_max_value = max(A(A~=max(A)))

Here A(A~=max(A)) will be temporary array that not contain maximal value of original array. Than you receive maximum of this array.

like image 198
Danil Asotsky Avatar answered Jan 05 '23 16:01

Danil Asotsky