From: Cesare on
"Steven Lord" <slord(a)mathworks.com> wrote in message <i0vf7j$8s9$1(a)fred.mathworks.com>...
>
> "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
>

Hi, thank you! Do you have any suggestion on what's the best way to compute the tolerance threshold?
Cheers,
Cesare
From: someone on
"Cesare " <cmfornewsgroup(a)gmail.com> wrote in message <i0viqk$9i1$1(a)fred.mathworks.com>...
> "Steven Lord" <slord(a)mathworks.com> wrote in message <i0vf7j$8s9$1(a)fred.mathworks.com>...
> >
> > "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
> >
>
> Hi, thank you! Do you have any suggestion on what's the best way to compute the tolerance threshold?
> Cheers,
> Cesare

The "best way" is simply the round-off error (someTolerance) YOU are willing to accept.
From: someone on
"Cesare " <cmfornewsgroup(a)gmail.com> wrote in message <i0vehl$mf5$1(a)fred.mathworks.com>...
> "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

% OK, then how about:

x = 1:10:51;
d = unique(x(2:end) - x(1:end-1))/100
From: Greg Heath on
On Jul 6, 9:33 am, "Cesare " <cmfornewsgr...(a)gmail.com> wrote:
> 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

N = length(x)
dx = (x(end)-x(1))/(N-1)
xnew = dx*(0:N-1);

Hope this helps.

Greg
From: Cesare on
Greg Heath <heath(a)alumni.brown.edu> wrote in message <b966a3aa-216c-4c40-9429-156c7b05c0ae(a)z8g2000yqz.googlegroups.com>...
> On Jul 6, 9:33 am, "Cesare " <cmfornewsgr...(a)gmail.com> wrote:
> > 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
>
> N = length(x)
> dx = (x(end)-x(1))/(N-1)
> xnew = dx*(0:N-1);
>
> Hope this helps.
>
> Greg


Dear Greg and "someone",
many thanks for your replies. Unfortunately I cannot ask the users of my function to input arrays that are built so that the numerical error does not show out. They'll generate x = start:step:stop and my function must be able to interpret is as an equispaced array correctly. Obviously, as Steven pointed out, one needs to set a tolerance. However I'm wondering whether for arrays generated as start:step:stop there is an "intrinsic" tolerance level that can be estimated. I don't know if this is related, however I've noticed that for

x = start:step:stop;
d = x(2:end) - x(1:end-1);
e = eps(d);

then unique(e) is tipically (well at least for my random tests) a scalar. I was wondering whether an observation like this or some other idea could be used to set the smallest possible threshold for determining whether x could be of the form start:step:stop.
All the best,
Cesare