i want to find maximum and minimum values in a matrix continuosly
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
LISSA DUVVU
am 28 Dez. 2019
Beantwortet: Chien-Han Su
am 28 Dez. 2019
i want to find maximum and minimum values in a matrix
suppose i have data like
1
2
5
6
3
2
5
8
9
1
so my minimum is 1 and maximum is 6 and next minimum is 2 maximum is 9 again 1 like that
i want result as
1
6
2
9
1
0 Kommentare
Akzeptierte Antwort
Chien-Han Su
am 28 Dez. 2019
Try this
% define an arbitray array a
a = [1, 2, 5, 6, 3, 2, 5, 8, 9, 1];
if length(a) <= 2
extreme = a;
else
extreme = zeros(1,length(a));
extreme(1) = a(1); % first element must be extreme value
count = 1; % count the number of extreme value
if a(2) > a(1)
increase = true; % use 'increase' to record the trend of increasing or decreasing
else
increase = false;
end
for n = 3:length(a)
if a(n) > a(n - 1)
if increase == false
count = count + 1;
extreme(count) = a(n - 1);
end
increase = true;
else
if increase == true
count = count + 1;
extreme(count) = a(n - 1);
end
increase = false;
end
end
extreme(count + 1) = a(end); % last elements must be extreme value
extreme = extreme(1:count + 1);
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!