i want to find maximum and minimum values in a matrix continuosly

2 views (last 30 days)
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

Accepted Answer

Chien-Han Su
Chien-Han Su on 28 Dec 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

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!