From: Cesare on
Hi! This has probably been asked several time however I couldn't find a post about it.
I need to check whether an input array is equispaced. However

x = 0.01:0.1:0.51;
d = x(2:end) - x(1:end-1);

won't result in a single valued unique(x) because of numerical errors. What is the best way to tackle this problem?
Many thanks in advance,
Cesare
From: someone on
"Cesare " <cmfornewsgroup(a)gmail.com> wrote in message <i0vbah$dc3$1(a)fred.mathworks.com>...
> Hi! This has probably been asked several time however I couldn't find a post about it.
> I need to check whether an input array is equispaced. However
>
> x = 0.01:0.1:0.51;
> d = x(2:end) - x(1:end-1);
>
> won't result in a single valued unique(x) because of numerical errors. What is the best way to tackle this problem?
> Many thanks in advance,
> Cesare

% One solution:

x = 1:10:51;
d = (x(2:end) - x(1:end-1))/100;
From: Andy on
"someone" <someone(a)somewhere.net> wrote in message <i0vbt7$lli$1(a)fred.mathworks.com>...
> "Cesare " <cmfornewsgroup(a)gmail.com> wrote in message <i0vbah$dc3$1(a)fred.mathworks.com>...
> > Hi! This has probably been asked several time however I couldn't find a post about it.
> > I need to check whether an input array is equispaced. However
> >
> > x = 0.01:0.1:0.51;
> > d = x(2:end) - x(1:end-1);
> >
> > won't result in a single valued unique(x) because of numerical errors. What is the best way to tackle this problem?
> > Many thanks in advance,
> > Cesare
>
> % One solution:
>
> x = 1:10:51;
> d = (x(2:end) - x(1:end-1))/100;

As a sidenote:

help diff
From: Cesare on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i0vd4l$fdu$1(a)fred.mathworks.com>...
> "someone" <someone(a)somewhere.net> wrote in message <i0vbt7$lli$1(a)fred.mathworks.com>...
> > "Cesare " <cmfornewsgroup(a)gmail.com> wrote in message <i0vbah$dc3$1(a)fred.mathworks.com>...
> > > Hi! This has probably been asked several time however I couldn't find a post about it.
> > > I need to check whether an input array is equispaced. However
> > >
> > > x = 0.01:0.1:0.51;
> > > d = x(2:end) - x(1:end-1);
> > >
> > > won't result in a single valued unique(x) because of numerical errors. What is the best way to tackle this problem?
> > > Many thanks in advance,
> > > Cesare
> >
> > % One solution:
> >
> > x = 1:10:51;
> > d = (x(2:end) - x(1:end-1))/100;
>
> As a sidenote:
>
> help diff

Thank you very much for your reply. And sorry for the error above, I mean that unique(d) is not a scalar. I've tried the solutions you proposed (dividing by 100 and using diff). However, I still get a d array with at least 3 non unique fields. Any suggestion?
Best,
Cesare
From: Steven Lord on

"Cesare " <cmfornewsgroup(a)gmail.com> wrote in message
news:i0vbah$dc3$1(a)fred.mathworks.com...
> Hi! This has probably been asked several time however I couldn't find a
> post about it.
> I need to check whether an input array is equispaced. However
>
> x = 0.01:0.1:0.51;
> d = x(2:end) - x(1:end-1);
>
> won't result in a single valued unique(x) because of numerical errors.
> What is the best way to tackle this problem?

Change your definition of "equispaced" to include vectors where the
differences between elements are "close enough" to the same rather than
being exactly, bit-for-bit, identical.

someTolerance = <fill in your value>
isEquispaced = (max(d) - min(d)) < someTolerance;

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com