フィルターのクリア

How to find second largest value in an array?

383 ビュー (過去 30 日間)
Minu
Minu 2013 年 6 月 7 日
回答済み: Anil Kumar 2022 年 6 月 29 日
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 件のコメント
Walter Roberson
Walter Roberson 2013 年 6 月 7 日
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 日
[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 件のコメント
Jonnathan  Cespedes
Jonnathan Cespedes 2018 年 2 月 14 日
@Andrei Bobrov, how can i do it for each row in a for loop?
Walter Roberson
Walter Roberson 2019 年 2 月 10 日
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 日
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 件のコメント
Srinivasa Rao Konda
Srinivasa Rao Konda 2020 年 10 月 10 日
How to find out the index of second largest element?
Walter Roberson
Walter Roberson 2020 年 10 月 10 日
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 日
If you're using release R2017b or later, consider using the maxk or mink functions.
  1 件のコメント
Pen-Li (Ben) Yu
Pen-Li (Ben) Yu 2021 年 11 月 3 日
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 日
for K = A
if sum(A > K) == 1
disp(K)
end
end
  1 件のコメント
per isakson
per isakson 2013 年 6 月 7 日
I got your point.

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


Dedaunan Rimbun
Dedaunan Rimbun 2017 年 11 月 5 日
編集済み: Dedaunan Rimbun 2017 年 11 月 5 日
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 日
try this
a=A(:,1);
[p,q]=max(a);
b=A(q,1);
[p1,q1]=max(a(a~=b))

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by