MATLAB Answers

find values from array

1 view (last 30 days)

Accepted Answer

Mark Rzewnicki
Mark Rzewnicki on 24 Oct 2019
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')

More Answers (1)

Bhaskar R
Bhaskar R on 24 Oct 2019
Edited: Bhaskar R on 24 Oct 2019
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 Comment
DP
DP on 24 Oct 2019
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,

Sign in to comment.

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!