How to check monotonity of a vector?
156 ビュー (過去 30 日間)
古いコメントを表示
How to check easily that components of a vector form a monotone (increasing) sequence or not?
1 件のコメント
Adam
2017 年 12 月 15 日
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.
採用された回答
Walter Roberson
2017 年 12 月 15 日
issorted() with 'ascend' (repeats permitted) or 'strictascend' (repeats not permitted)
0 件のコメント
その他の回答 (1 件)
KL
2017 年 12 月 15 日
編集済み: KL
2017 年 12 月 15 日
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 件のコメント
Andreas
2022 年 3 月 16 日
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))
isIncreasingB = all(diff(b))
To fix that, restrict to positive values of diff
isIncreasingA = all(diff(a)>0)
isIncreasingb = all(diff(b)>0)
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!