Reputation: 191
I have a question regarding the interpolation of some vectors which can not be monotonic.
The data vectors looks as follow:
x = x1 = y =
20.0000 21.6000 32
21.8000 19.8000 132
22.2000 18.0000 193
21.4000 16.6000 351
20.2000 17.0000 576
20.6000 16.0000 649
20.3000 13.4000 686
19.4000 12.2000 806
16.9000 11.4000 1117
15.8000 11.2000 1252
15.6000 10.9000 1281
15.3000 9.7000 1379
14.8000 9.2000 1527
14.5000 8.7000 1577
12.4000 7.2000 1943
11.8000 5.0000 2047
10.4000 3.0000 2282
5.3000 2.1000 2840
3.5000 2.0000 3047
2.6000 1.8000 3140
(small part)
I would link to achieve 'y1' as interpolation of these using:
y1 = interp1(x,y,x1);
but x and x1 are not monotonic.
y1 should be as long as y
Have you an idea of how to perform the interpolation?
Upvotes: 0
Views: 1235
Reputation: 1257
Sort both y and x such as x is monotonic. than sort x1 and use it as presented.
See if the code below helps.
[new_x,indx]=sort(x);
new_y=y(indx);
new_x1=sort(x1);
%solves duplicate entries through the unique function (1) or average entries (2)
[temp_new_x,indx]=unique(new_x);
% (1) discard all repeated x values except the last one
new_y=new_y(indx);
new_x=temp_new_x;
% (2) average repeated entries
new_y = arrayfun(@(C) mean(new_y(C==new_x)),temp_new_x);
new_x=temp_new_x;
y1=interp1(new_x,new_y,new_x1);
Upvotes: 2
Reputation: 2131
Sort the x data and re-index the y data using the results, then interpolate:
[sortedX, IX] = sort(x);
y1 = interp1(sortedX, y(IX), X1);
Upvotes: 1