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

1 次查看(过去 30 天)
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

采纳的回答

Chien-Han Su
Chien-Han Su 2019-12-28
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 个)

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by