Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
SL
SL on 24 Oct 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (2)

Guillaume
Guillaume on 24 Oct 2016
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 Comments
Marc Jakobi
Marc Jakobi on 25 Oct 2016
Edited: Marc Jakobi on 25 Oct 2016
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 on 25 Oct 2016
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 on 24 Oct 2016
Edited: Marc Jakobi on 24 Oct 2016
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 Comments
Guillaume
Guillaume on 24 Oct 2016
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 on 24 Oct 2016
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.

Products

Community Treasure Hunt

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

Start Hunting!