Effacer les filtres
Effacer les filtres

How to check monotonity of a vector?

154 vues (au cours des 30 derniers jours)
Mr M.
Mr M. le 15 Déc 2017
Commenté : Andreas le 16 Mar 2022
How to check easily that components of a vector form a monotone (increasing) sequence or not?
  1 commentaire
Adam
Adam le 15 Déc 2017
You can use
validateattributes( yourVector, { 'numeric' }, { 'vector', 'increasing' } )
if you just want to validate that it is monotonic and carry on with the code if it is while throwing an error otherwise.

Connectez-vous pour commenter.

Réponse acceptée

KL
KL le 15 Déc 2017
Modifié(e) : KL le 15 Déc 2017
use diff
along with all maybe
a = 1:10;
isIncreasing = all(diff(a)) %or all(diff(a)>=0) if you want to allow 0 difference
  1 commentaire
Andreas
Andreas le 16 Mar 2022
Take care, that all is also true for negative values.
Therefore, this example has a false positive on b
a = [1:10];
b = [1 2 3 2 1 5];
isIncreasingA = all(diff(a))
isIncreasingA = logical
1
isIncreasingB = all(diff(b))
isIncreasingB = logical
1
To fix that, restrict to positive values of diff
isIncreasingA = all(diff(a)>0)
isIncreasingA = logical
1
isIncreasingb = all(diff(b)>0)
isIncreasingb = logical
0

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 15 Déc 2017
issorted() with 'ascend' (repeats permitted) or 'strictascend' (repeats not permitted)

Catégories

En savoir plus sur Files and Folders dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by