Equivalent of interp1 for several sets of sampling (same data).

1 view (last 30 days)
I would like to get over the interp1 limitation that x has to be a vector.
More clearly, I would like to interpolate the same data from several possible x vectors.
Something like
interp1(x,v,xq)
But for which x would be a matrix where each column is a possible vector. Something like that but without the loop:
x = [[1;2;3],[1.1;2.1;3.5],[0.9;2;2.7]];
for i = 1:size(x,2)
interp1(x(:,i),v,xq);
end;
Like I'm not really sure what my original x axis is, so I want to compare what it gives for some reference xq points...
Any idea?? Thanks in advance.

Answers (1)

John D'Errico
John D'Errico on 5 Jun 2018
Sorry, but that is a limitation for a very good reason. They are different curves when you change x.
Interp1 interpolates values through a given curve. Change the curve? You need to call interp1 again.
Is a loop a bad thing here? Why should it be so? If you have multiple distinct curves, then interp1 would just need to have an internal loop anyway! So just use a loop. You already have a very simple solution. It just uses a loop.
  2 Comments
The Gogo
The Gogo on 5 Jun 2018
Edited: The Gogo on 5 Jun 2018
Oki, thx. Maybe I'm indeed overcomplicating my life... Isn't the curve creation something which is always the same, so can be done in parallel for all my sets of x? no idea how it works.
Just I'm going to call this in an optimization algorithm that is going to provide me iteratively a lot of sets of x, so if I can find a way to save some resource...
John D'Errico
John D'Errico on 5 Jun 2018
Edited: John D'Errico on 5 Jun 2018
Well, IF you were using a spline, then it would be more reason to say this is not possible. You are using the default, a linear interpolant, so something fairly basic. But at its heart, this is still a nonlinear operation, involving a search for which interval a value lines in, and then doing linear interpolation on that interval. Can I think of a tricky way where you could solve this? Um, yeah, probably. Is it worth it? Is it worth the effort? Um, well, probably not. At this time of the morning? Are you kidding? :)
Seriously, it would probably take a few more lines to write than it would to just write the loop, and be no more efficient and WAY less readable as to what the code does. I'd still be writing the explanation as to what it does and why it works, because people always ask for that. In fact, even if I WERE the author of interp1 (I'm not) AND I wanted to allow that as an option, I would just solve it as an internal loop inside interp1.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!