how can we calculate interpolation between two double MATLAB arrays?

Given two arrays of double of different sizes (A = 1054x1 and B = 601x1) how is it possible to perform an interpolation ??

 Akzeptierte Antwort

It depends on how you want to interpolate them. The‘safest’ way is likely to interpolate the longer vector to the length of the shorter vector:
A = sin((0:1053)/105)';
B = cos((0:600)/30)';
Ai = interp1((1:numel(A))', A, (1:numel(B)), 'linear', 'extrap');
You can then see the result (a sort of Lissajous figure):
figure
plot(Ai, B)
grid

8 Kommentare

and if I wanted to do a linear interpolation?
My example specifically does linear interpolation.
If you want other types of interpolation, see the method (link) documentation section for the available options. (Using 'extrap' prevents some beginning and ending values from being NaN, however it is necessary to be careful in doing any sort of extrapolation, and not to extrapolate far beyond the region of the data you know to be correct.)
ok. Last question. and if I didn't want to see a sinusoidal trend in the graph?
My example simply uses sinusoids for illustration, since those are easier to visualise than random data, and so more informative. Your ‘A’ and ‘B’ vectors can be anything you define them to be. The code will work successfully regardless.
You can also interpolate ‘B’ to be the size of ‘A’, although that involves creating data (here 453 points, almost doubling its size), all of which assume it behaves in those regions the way it behaves in the regions you know. That’s the reason I always suggest extrapolating the longer vector to the dimension of the shorter vector. You already know what it does, so there are fewer assumptions to be made.
Guido Pastore
Guido Pastore am 15 Mär. 2019
Bearbeitet: Guido Pastore am 15 Mär. 2019
But if I wanted to visualize random and non-sinusoidal data ??? If I didn't want to use cosine and sine what instructions should I use to perform the interpolation ??? I'm not very practical with Matlab I'm sorry
Just use whatever your vectors are. The sinusoids are irrelevant.
My code would work as well with:
A = rand(1054, 1);
B = rand(601,1);
Ai = interp1((1:numel(A))', A, (1:numel(B)), 'linear', 'extrap');
or anything else your vectors have. It simply would have been more difficult graphically to demonstrate the effect.
Thank you so much for your help
As always, my pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by