How to find second largest value in an array?

Hi
I want to find the second largest and minimum value in array? A=[1;10;15;30;40;50;100].I want to get the result as Second minimum value=10 and second largest value=50 Help me plz...

댓글 수: 1

What do you want to do if there are multiple instances of the maximum or minimum?

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 6월 7일
편집: Andrei Bobrov 2013년 6월 7일

4 개 추천

[ii,ii] = sort(A);
out = A(ii([2,end-1]));
for your example (A) just:
out = A([2,end-1]);
more variant
A1 = unique(A);
out = A1([2,end-1]);

댓글 수: 8

Be careful for the case where A only has one element. And cases where A has duplicate elements.
Jan
Jan 2013년 6월 7일
편집: Jan 2013년 6월 7일
The sorting can be more expensive than searching the max twice:
[ignore, index] = max(A);
A(index) = -Inf; % [EDITED], not +Inf!
max2 = max(A);
In this method Walter's suggestions must be considered also.
If the array is single precision or double precision, NaN is safer than Inf as there might be Inf in the array.
If the array is any of the other numeric data types, Inf will not exist and will be treated as the maximum numeric value for that datatype.
Jan
Jan 2013년 6월 7일
편집: Jan 2017년 10월 14일
Fixed typo: Of course -Inf is needed instead of +Inf to mask an existing maximium.
@Walter: Exactly. While the original message "A=[1;10;15;30;40;50;100]" looks like the OP talks about a double vector with finite values, the another type of input or values must be considered.
Even if another value is -Inf, my method replies the correct result. But NaN is the better choice.
MoonPie1
MoonPie1 2015년 12월 28일
How to get the index with the second minimum or maximum value in an array?
Image Analyst
Image Analyst 2015년 12월 28일
Define second minimum. In the array [1,1,2,3] what is the second minimum? Is it 1 or is it 2?
@Andrei Bobrov, how can i do it for each row in a for loop?
Amjad Iqbal comments,
Hello Researchers!! I need guidance, as i have a matrix H1 of 1576*1024, which is vertical concatination of four channels, in H1 for continuous four rows it represent one frame of each channel, i need to find maximum and second value for every four group of rows. untill now i just get to find maximum and minimum value for each row. kindly guide me, how can i apply 2nd loop or design function, so that i can get results of second maximum for each four group of rows, and in total i will have 394 max and 394 second maximum results.
H1 = vertcat(A,B,C,D)
temp_A = zeros(1576,2);
for N = 1:1:1576
temp_A(N,1) = max(H1(N,:));
temp_A(N,2) = min(H1(N,:));
end

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

추가 답변 (5개)

Fernando
Fernando 2014년 11월 17일
편집: Walter Roberson 2017년 10월 14일

20 개 추천

function [ y ] = second_min( x )
y = min(x(x>min(x)));
end
&
function [ y ] = second_max( x )
y = max(x(x<max(x)));
end

댓글 수: 6

Shuang Liu
Shuang Liu 2017년 10월 14일
beautiful
Thanks
Satya G
Satya G 2019년 2월 6일
Could you please explain how "max(x(x<max(x)))" works?
x<max(x) is giving array of all ones and one zero. How these will be used ?
Aäron Penders
Aäron Penders 2019년 2월 26일
편집: Aäron Penders 2019년 2월 26일
@Satya G, in case this isn't cleared up yet, i'll explain. It works exactly as you said. "x<max(x)" gives a logical array of ones where x is less than the maximum, and zero where the maximum is found. The zero therefore corresponds to the maximum. Next you give this logical array as an input argument to the data x itself, which returns all values of x where the logical array is equal to one. The value of the maximum where the logical index is zero, is left out. Afterwards the final max() finds the maximum of the above, which corresponds to the second maximum. Look up logical indexing in matlab for more details.
Quite a clever solution, props.
How to find out the index of second largest element?
Define second largest. In the array [1,2,3,3] what is the second largest? Is it 2 or is it 3?

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

Steven Lord
Steven Lord 2017년 11월 6일

13 개 추천

If you're using release R2017b or later, consider using the maxk or mink functions.

댓글 수: 1

A=[1;10;15;30;40;50;100];
Amax2 = maxk(A,2); Amax2(2)
ans = 50
Amin2 = mink(A,2); Amin2(2)
ans = 10

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

Walter Roberson
Walter Roberson 2013년 6월 7일

3 개 추천

for K = A
if sum(A > K) == 1
disp(K)
end
end
Dedaunan Rimbun
Dedaunan Rimbun 2017년 11월 5일
편집: Dedaunan Rimbun 2017년 11월 5일

0 개 추천

I think this one is the best. It has some pros:
  1. You can specify the largest, second largest, third largest, and so on.
  2. You can also see the index
Note: I just modify what Walter Roberson suggest.
for k=1:s %s is the number highest number will be shown
k2=0;
for K = A'
k2=k2+1;
if sum(A > K) == k-1
M(k,1)=K;
I(k,1)=k2;
end
end
end
Anil Kumar
Anil Kumar 2022년 6월 29일

0 개 추천

try this
a=A(:,1);
[p,q]=max(a);
b=A(q,1);
[p1,q1]=max(a(a~=b))

카테고리

태그

질문:

2013년 6월 7일

답변:

2022년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by