Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to get 1807 point data array to 1806 data array?

조회 수: 1 (최근 30일)
SL
SL 2016년 10월 24일
마감: MATLAB Answer Bot 2021년 8월 20일
I thought the process would go with interpolation like the following but not
u = interp1(u, size(u) - 1);
which gives me `NaN [integer]`, so wrong.
I do not want to lose the accuracy of the results but I need to compare the vector with its original vector which has one point less.
MATLAB: 2016b

답변 (2개)

Guillaume
Guillaume 2016년 10월 24일
As Marc says, you need a vector for the query points but his solution for generated that vector does not make much sense.
A good way of generating one less query point than the original is to use linspace:
uq = interp1(u, linspace(1, numel(u), numel(u)-1))
That's assuming that the original u correspond to x = 1:numel(u).
  댓글 수: 3
Marc Jakobi
Marc Jakobi 2016년 10월 25일
편집: Marc Jakobi 2016년 10월 25일
SL, the default (linear) interp1 is very simple. It creates a straight line between two points and returns the value of the line at the position of the query points specified in Guillaume's example by
linspace(1, numel(u), numel(u)-1))
What the above line does is create query points with the length of u minus 1 spaced equally between 1 and the length of u. So if u has a length of 5, the indexes of u would be
[1 2 3 4 5]
and
linspace(1, numel(u), numel(u)-1))
would return
[1 2.33333333333333 3.66666666666667 5]
So instead of taking the points at 1, 2, 3, 4 and 5, you are taking the linearly interpolated points at 1, 2.33, 3.67 and 5.
It is impossible to avoid "losing accuracy". However, this can work well with little accuracy loss if u is a very long vector, e. g. if u has a length of 1087, the query points would be at
[1, 2.0009, 3.0018, ..., 1085.9991, 1087]
If you want to find out more details on how the functions work, I would recommend taking a look at the Matlab documentation and if you want more details about the algorithms, there are many books on "Numerical Methods".
P. S. if an answer helps you, you should accept it.
Guillaume
Guillaume 2016년 10월 25일
Yes, exactly what Marc said. There's nothing complicated about the code and certainly no non-linear regression. Just plain linear interpolation.

Marc Jakobi
Marc Jakobi 2016년 10월 24일
편집: Marc Jakobi 2016년 10월 24일
The second input must be a vector, not the size of the output. So if you want to interpolate between the points, use something like
uq = interp1(u, (1.5:length(u) - 0.5)); %assuming u is a vector
  댓글 수: 2
Guillaume
Guillaume 2016년 10월 24일
1.5:size(u) - 0.5
Gah! While matlab will accept this syntax it is extremely unlikely it will do what you mean.
size will return a vector with at least 2 elements (even for vectors) and more if u has more than two dimension. : will only use the first of these, which if u is a row vector will be 1. Therefore the above will generate an empty vector.
Marc Jakobi
Marc Jakobi 2016년 10월 24일
woops, wasn't paying proper attention there.
it should be
1.5:length(u)-0.5
(assuming u is a vector) Thanks for pointing it out.

이 질문은 마감되었습니다.

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by