Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
SL
SL le 24 Oct 2016
Clôturé : MATLAB Answer Bot le 20 Août 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

Réponses (2)

Guillaume
Guillaume le 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 commentaires
Marc Jakobi
Marc Jakobi le 25 Oct 2016
Modifié(e) : Marc Jakobi le 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 le 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 le 24 Oct 2016
Modifié(e) : Marc Jakobi le 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 commentaires
Guillaume
Guillaume le 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 le 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.

Cette question est clôturée.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by