Indexing vectors with a zero and creating a "predicted" vector knowing what the diff should be
Show older comments
I have some measured values as below (that should be multiples of some fixed value)
xmeasured =
-52.9734
-39.7061
-26.4657
-13.0291
0
13.0255
26.4704
39.5217
52.9689
I now what the real seperation between each element should be (and it should be fixed) at 13.1459
I want to create another Column (or vector) with the following "predicted" values where either side of zero, the values are multiples of 13.1459, i.e
Predicted =
(-3 x 13.1459)
(-2 x 13.1459)
(-1 x 13.1459)
-13.1459
0
13.1459
(2 x 13.1459)
(3 x 13.1459)
So I can then calculate the difference between measured and predicted.
My data can be any size and the 0 isn't necessarily in the middle (although it is always present somewhere)
3 Comments
xmeasured = [ ...
-52.9734;
-39.7061;
-26.4657;
-13.0291;
0;
13.0255;
26.4704;
39.5217;
52.9689];
polyfit(-4:4, xmeasured, 1)
Looks like 13.1459 is not an especially good predictor.
Accepted Answer
More Answers (2)
If you believe they are uniformly spaced, the isuniform function can confirm this (and give you the uniform spacing) or refute it (and tell you the spacing is NaN.) Your vector isn't uniformly spaced, as you can see by inspection from the difference in the absolute values of the two elements surrounding the 0 in your data. isuniform correctly says that it is not uniformly spaced.
xmeasured = [ ...
-52.9734;
-39.7061;
-26.4657;
-13.0291;
0;
13.0255;
26.4704;
39.5217;
52.9689];
[isUniformlySpaced, stepsize] = isuniform(xmeasured)
If you want them to be an ideal distance apart, figure out what multiple of that ideal distance each element is:
idealSpacing = 13.1459;
multipliers = xmeasured ./ idealSpacing
You can round the multipliers to the nearest integer then multiply by the spacing.
xideal = round(multipliers)*idealSpacing
xideal ought to be uniform from the way it was constructed, and isuniform confirms it is:
[isIdealUniform, spacing] = isuniform(xideal)
Is the spacing calculated by isuniform the same as the spacing used to create xideal?
spacing - idealSpacing
How different are the measured and ideal vectors?
D = xmeasured - xideal
1 Comment
Here's an approach using the an integer-constrained linear fitting routine minL1intlin, downloadable from
Basically, if t=13.1459 is the true, known spacing, we can formulate the problem as solving for the unknown integer shift s that best fits the linear equations,
xmeasured = t*(1:n)'-t*s
and subject to the constraints 1<=s<=n. The latter ensures that zero is somewhere in the prediction, xpredicted.
xmeasured =[-52.9734
-39.7061
-26.4657
-13.0291
0
13.0255
26.4704
39.5217
52.9689];
t=13.1459;
n=numel(xmeasured);
xt=(1:n)'*t;
e=ones(n,1)*t;
s=round(minL1intlin(-e,xmeasured-xt,1,[],[],[],[],1,n))
xpredicted=xt-t*s
residuals=xmeasured-xpredicted
1 Comment
Categories
Find more on Descriptive Statistics and Insights in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!