find values from array
조회 수: 1 (최근 30일)
이전 댓글 표시
채택된 답변
Mark Rzewnicki
2019년 10월 24일
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')
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!