Indexing vectors with a zero and creating a "predicted" vector knowing what the diff should be
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Walter Roberson
2025 年 6 月 25 日
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.
採用された回答
Paul
2025 年 6 月 25 日
This solution is similar in spirit to that in this comment, which I think is the best approach based on how the problem has been described, particularly that the xmeaured data can stray more than one bin away from the uniformly spaced prediction.
xmeasured = [ ...
-52.9734;
-39.7061;
-26.4657;
-13.0291;
0;
13.0255;
26.4704;
39.5217;
52.9689];
idx = find(xmeasured==0);
n = (1:numel(xmeasured)).'-idx;
xuni = n*13.1459;
[xmeasured,xuni]
2 件のコメント
Walter Roberson
2025 年 6 月 25 日
I have to wonder whether it is certain that there will be an exact 0, and not an approximate 0 instead...
Steven Lord
2025 年 6 月 25 日
In that case you could use min with ComparisonMethod "abs" to locate the smallest magnitude value in the array.
xmeasured = [ ...
-52.9734;
-39.7061;
-26.4657;
-13.0291;
-0.001; % not exactly 0
13.0255;
26.4704;
39.5217;
52.9689];
[minValue1, minLocation1] = min(xmeasured, [], ComparisonMethod = "abs")
In releases prior to the introduction of that option you could take the abs of the data before calling min but then you'd have to potentially negate the result. Compare the signs of minValue1 and minValue2.
[minValue2, minLocation2] = min(abs(xmeasured))
The ComparisonMethod is necessary in this case. For real data, by default MATLAB looks for the smallest real part of your data.
[minValue3, minLocation3] = min(xmeasured)
その他の回答 (2 件)
Steven Lord
2025 年 6 月 25 日
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
Matt J
2025 年 6 月 25 日
編集済み: Matt J
2025 年 6 月 26 日
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!