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

3 vues (au cours des 30 derniers jours)
cestcandice
cestcandice le 7 Juin 2017
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.

Réponses (3)

Star Strider
Star Strider le 7 Juin 2017
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 le 7 Juin 2017
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 le 7 Juin 2017
Since this is related to a homework question I'm only going to give a hint: you may find the cumulative maximum function useful.

Community Treasure Hunt

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

Start Hunting!

Translated by