diff isEquidistant.m @ 487:b43c4d841afe

Add isEquidistant function for testing vectors
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 08 Aug 2017 08:26:25 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/isEquidistant.m	Tue Aug 08 08:26:25 2017 +0200
@@ -0,0 +1,19 @@
+% Tests if consecutive elements of vector v are euidistant
+function b = isEquidistant(v)
+    if length(v) < 2
+        error('sbplib:isEquidistant:inputTooShort', 'Input vector is too short');
+    end
+
+    tol = 1e-8;
+
+    d = v(2:end) - v(1:end-1);
+    err = abs(d - d(1));
+
+    relErr = err./abs(d);
+
+    I_zero = find(d < tol);
+
+    relErr(I_zero) = err(I_zero);
+
+    b = all(relErr < tol);
+end