Using interp1 with multiple data that meets criteria - Looking to have interp 1 return the max value

6 Ansichten (letzte 30 Tage)
I've got an interesting situation where I'm trying to interpolate some datapoints, and the dataset I have has multiple points which would meet the interpolation criteria....
For a dummy dataset, lets look at a set of 10 points
DATA = [1, 1, 1;
2, 2, 2;
3, 3, 3;
4, 4, 4;
5, 5, 5;
6, 4.01, 10;
7, 3.01, 15;
8, 4.02, 12;
9, 5.02, 10;
10, 4.03, 7];
Think of column 1 as "time"
I've then got code for the interpolation portion which reads like this:
INTERP_POINT = X.XX;
INTERP_DATA = interp1(DATA(:,2),DATA(:,3),INTERP_POINT);
Now, if I want to interpolate at 1.5, no problem, the code returns 1.5 given the example. I'm running into a problem when I'm trying to grab a value for say 4.5. I need it to give me the max value that 4.5 could be, if MATLAB interpolates between time 4/5 it would return values of 4.5. but what I'm trying to get it to do is return the value of ~11 which would come between time 7/8.
I'm not convinced that interp1 is the correct way to do this, but I'm coming up blank on alternatives. I need it to interpolate at each point in "time" that meets the criteria and return the max value. The code currently just returns the first instance of this it finds.
Any help would be GREATLY appreciated!
-Brad
  5 Kommentare
dpb
dpb am 13 Sep. 2018
Bearbeitet: dpb am 13 Sep. 2018

That confirms my note below that the correct answer for the example given then is between 9-10 and not as requested (and my solution forced) between 8-9.

To find the last something like

>> ix2=find(abs(diff(sign([nan; diff(DATA(:,2)-ix)])))==2,1,'last')+1;
>> ix1=ix2-1;
>> interp1(DATA(ix1:ix2,2),DATA(ix1:ix2,1),ix)
ans =
  9.5253
>> 

based on the same general idea as before.

NB: This is predicated that the "time" column is uniformly nondecreasing; otherwise the search has to be on magnitude not position.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 13 Sep. 2018
Your example code returns
ix=4.5;
interp1(DATA(:,2),DATA(:,3),ix)
ans =
6.0309
rather than 4.5; that result would require
interp1(DATA(:,1),DATA(:,2),ix)
instead.
With a multi-valued function you'll have to do some bracketing to get within the desired range. To get the largest "time" that corresponds to the desired range you'll have to bracket those locations and use them to restrict the interpolation.
ix2=find(DATA(:,2)>ix,1,'last'); % last point > target
ix1=find(DATA(1:ix2,2)<ix,1,'last'); % last point < target prior to previous
Vq=interp1(DATA(ix1:ix2,2),DATA(ix1:ix2,1),ix)
Vq =
8.4800
It'll take some additional logic to ensure the lookup points actually bracket the value are such that ix1<=ix2, etc., depending on just what the actual data table looks like.
It may be simpler to just do a direct calculation with a "search and destroy" technique given the nature of the sample data rather than trying to use interp1 where it doesn't really fit the problem.
  2 Kommentare
Brad_A
Brad_A am 13 Sep. 2018
Can you provide some insight on the search and destroy technique you're recommending?
dpb
dpb am 13 Sep. 2018

Nothing fancy; just as was writing the above and without more specific cases to think about what are all the possible results for the bracketing it seemed as though might turn out that just writing the explicit interpolation expression might be as easy as building the correct sub-vector.

You'll have to consider the nature of your problem as to what cases will possibly occur; for example with the above sample if search for the last < wanted first it will find the result between 9-10, not the stated case of the one between 8-9.

Now, maybe that was a misstatement regarding what the proper answer really should be for the case if it is, indeed, the last valid interval that is wanted. That's why I was so nebulous in the comments; as Stephen noted, it isn't fully clear just what the criteria are for how to choose which interval is "the right stuff".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by