Count Each Maximum Number in an Array That's Larger Than Previous Max Number

4 次查看(过去 30 天)
For example if I have
a=[1,2,4,3,4,7,4,9,3,4,10,4] then the numbers I would need to count are 1,2,4,7,9,10
So this isn't necessarily looking at if the number is larger than the number before it, but if it's larger than the previous maximum number.
This is not my actual homework, I'm just using this as an example so I can apply it my homework.

回答(3 个)

Star Strider
Star Strider 2017-6-7
There are probably a number of different ways to do this.
One approach:
a=[1,2,4,3,4,7,4,9,3,4,10,4];
q = [];
for k1 = 1:length(a)
q(k1) = max(a(1:k1));
end
Out = unique(q)
Out =
1 2 4 7 9 10

Image Analyst
Image Analyst 2017-6-7
A simple for loop should do it:
newMax = [];
for k = 1 : length(a)
if ......
newMax = [.....
end
end
March along the vector, building up the list of values "newMax" everytime you encounter a value more than newMax(end).
There are other ways but this might be the simplest.

Steven Lord
Steven Lord 2017-6-7
Since this is related to a homework question I'm only going to give a hint: you may find the cumulative maximum function useful.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by