I am given a vector of numbers and I have to check if this vector has its samples equispaced or not. How can I do it without using fors?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 12 日
編集済み: Ameer Hamza 2020 年 10 月 12 日

1 投票

Something like this
x = [1 2 3];
dx = diff(x);
is_equidistant = all(dx==dx(1));
Result
>> is_equidistant
is_equidistant =
logical
1
If
x = [1 2 5];
dx = diff(x);
is_equidistant = all(dx==dx(1));
Result
>> is_equidistant
is_equidistant =
logical
0
If you want to check it with a tolerance value
x = [1 2 3.00000000000001];
dx = diff(x);
is_equidistant = all(ismembertol(dx, dx(1)));
Result
>> is_equidistant
is_equidistant =
logical
1

1 件のコメント

Mireia Boneta Camí
Mireia Boneta Camí 2020 年 10 月 12 日
thank you very much!!!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 10 月 12 日
編集済み: Stephen23 2020 年 10 月 12 日

1 投票

>> v = [1,2,3,4,5,6,7];
>> x = ~any(diff(v,2))
x = 1
>> v = [1,2,4,4.5,7];
>> x = ~any(diff(v,2))
x = 0

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2020 年 10 月 12 日

編集済み:

2020 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by