How to find second largest value and its index in an array?
98 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I am trying to find the second largest value and its index in an array given by
A = [11,15,16,99,87]
Any help in this regard will be highly appreciated.
0 个评论
回答(2 个)
Image Analyst
2021-10-7
charu, did you try sorting and using find()? We also need to know your definition of "second largest".
% What is the second largest in this vector: 99 or 87?
A = [11,15,16,99,87, 99, 87]
% Assume it's 87, then to find all locations where 87 occurs:
sortedA = sort(unique(A), 'descend')
indexes = find(A == sortedA(2))
You get
A =
11 15 16 99 87 99 87
sortedA =
99 87 16 15 11
indexes =
5 7
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!