%% New in MATLAB R2022b: determine if a vector is uniformly spaced
% MATLAB Central Community Highlight;  Adam Danz, September 2022
%
%% Uniform spacing and the problem of round-off error
% The vector [3 4 5 6 7 8 9] is uniformly spaced with a step size of 1.  So is [3 2 1 0 -1 -2], with a step side of -1.
% The vector [1 2 4 8] is not uniformly spaced.
% A vector v with uniform spacing has the same finite interval or step size between consecutive elements of the vector. But sometimes round-off error poses a problem in calculating uniformity.
% Take, for example, the vector produced by

format shortg
v = linspace(1,9,7)

% Linspace produces linearly spaced vectors but the intervals between elements of v, computed by diff(v), are not identical.

dv = diff(v)
dv == dv(1)
diff(dv)

% Some extra steps are therefore necessary to set a tolerance that ignores error introduced by floating point arithmetic.
%% New in R2022b: isuniform
% Determining uniformity of a vector became a whole lot easier in MATLAB R2022b with the new isuniform function.
% [tf,step] = isuniform(v)
% isuniform returns a logical scalar indicating whether vector v is uniformly spaced within a round-off tolerance and returns the step size (or NaN if v is not uniform).
% Let's look at the results for our vector v,

[tf,step] = isuniform(v)

% How about non-uniformly spaced vector?

[tf,step] = isuniform(logspace(1,5,4))

%% Give it a shot in MATLAB R2022b
% What happens when all elements of v are equal?
% Can you produce a vector with uniform spacing without using colons or linspace?
% What additional steps would be needed to use isuniform with circular data?
%% References
% isuniform - documentation
% Floating point numbers - documentation
% Floating point numbers - Cleve's Corner (blog)
%% Learn about other new features
% Release notes
% What's new in MATLAB - download the latest release