MATLAB Answers

How to check monotonity of a vector?

153 views (last 30 days)
Mr M.
Mr M. on 15 Dec 2017
Answered: Walter Roberson on 15 Dec 2017
How to check easily that components of a vector form a monotone (increasing) sequence or not?
  1 Comment
Adam
Adam on 15 Dec 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.

Sign in to comment.

Accepted Answer

KL
KL on 15 Dec 2017
Edited: KL on 15 Dec 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

More Answers (1)

Walter Roberson
Walter Roberson on 15 Dec 2017
issorted() with 'ascend' (repeats permitted) or 'strictascend' (repeats not permitted)

Tags

Community Treasure Hunt

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

Start Hunting!