Effacer les filtres
Effacer les filtres

How to find a series of 'next max' and their indices in an array of numbers?

1 vue (au cours des 30 derniers jours)
Hi,
Suppose I have an array of numbers.
I want to find all the numbers, and their indices, which are largest then all previous ones.
For example, for the array:
X = [ 1 2 5 3 4 7 2 3 10 3]
I will get:
Largest: [1 2 5 7 10]
Indices: [1 2 3 6 9]
Sure I can solve it with a 'for' loop.
What will be the fastest way to do it in Matlab?
  2 commentaires
Fabio Freschi
Fabio Freschi le 4 Sep 2019
Note that it should be
Largest = [1 2 5 7 10]
Alon Rozen
Alon Rozen le 4 Sep 2019
Modifié(e) : Alon Rozen le 4 Sep 2019
Thanks :)
Corrected.

Connectez-vous pour commenter.

Réponse acceptée

Fabio Freschi
Fabio Freschi le 4 Sep 2019
Modifié(e) : Fabio Freschi le 4 Sep 2019
use cummax to find the cumulative maximum, then play with the solution using unique
>> Y = cummax(X)
Y =
1 2 5 5 5 7 7 7 10 10
>> [largest,index] = unique(Y)
largest =
1 2 5 7 10
index =
1
2
3
6
9

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by