find values from array

 采纳的回答

Here's a "quick and dirty" approach.
TT = [ 1 0 1 0 5 59 59 299 59 5 952 69 6 5 8 289 2 8 52 5 1 7 7 5 2 8 2 1 1 2 6 9 7 5 2 1000 5 8 95 1825];
NN = 1:1:40;
%% threeMax and twoMin will be used to store the max and min values
threeMax = zeros(1,3);
twoMin = 100*ones(1,2);
max1index = 0;
max2index = 0;
max3index = 0;
min1index = 0;
min2index = 0;
%% loop logic to rank largest 3 elements in descending order
for i = 1:1:length(TT)
if TT(i) > threeMax(1)
threeMax(3) = threeMax(2);
max3index = max2index;
threeMax(2) = threeMax(1);
max2index = max1index;
threeMax(1) = TT(i);
max1index = i;
elseif TT(i) > threeMax(2)
threeMax(3) = threeMax(2);
max3index = max2index;
threeMax(2) = TT(i);
max2index = i;
elseif TT(i) > threeMax(3)
threeMax(3) = TT(i);
max3index = i;
else
end
end
%% loop logic to locate the 2 smallest nonzero elements
for i = 1:1:length(TT)
if TT(i) < twoMin(1) && TT(i) > 0
twoMin(1) = TT(i);
min1index = i;
elseif TT(i) < twoMin(2) && TT(i) > 0
twoMin(2) = TT(i);
min2index = i;
else
end
end
stem(NN,TT);
hold on
stem([max1index max2index max3index],threeMax)
stem([min1index min2index],twoMin)
legend ('all values','max values','min values')

更多回答(1 个)

Bhaskar R
Bhaskar R 2019-10-24
编辑:Bhaskar R 2019-10-24
First 3 Maximum values
sorted_values_des = sort(A, 'descend');
maximun_3 = sorted_values_des(1:3);
Coming to minimum values
sorted_values_asc = sort(A);
values_gt_0 = sorted_values_asc(sorted_values_asc>0);
minimun_2 = values_gt_0(1:2);
Hope helps you !!

1 个评论

what abuot 2 minmum values(more than zeros) and please give me loop or function that do not change order in maximum and minimum .
moreover, it go through whole array,

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

版本

R2018a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by