how can we calculate interpolation between two double MATLAB arrays?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Given two arrays of double of different sizes (A = 1054x1 and B = 601x1) how is it possible to perform an interpolation ??
Accepted Answer
Star Strider
on 15 Mar 2019
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 Comments
Guido Pastore
on 15 Mar 2019
and if I wanted to do a linear interpolation?
Star Strider
on 15 Mar 2019
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.)
Guido Pastore
on 15 Mar 2019
ok. Last question. and if I didn't want to see a sinusoidal trend in the graph?
Star Strider
on 15 Mar 2019
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
on 15 Mar 2019
Edited: Guido Pastore
on 15 Mar 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
Star Strider
on 15 Mar 2019
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.
Guido Pastore
on 15 Mar 2019
Thank you so much for your help
Star Strider
on 15 Mar 2019
As always, my pleasure.
More Answers (0)
Categories
Find more on Interpolation in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)