Matlab:how to interpolate non motonic data

谁说胖子不能爱 提交于 2019-12-11 18:56:48

问题


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?


回答1:


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);



回答2:


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);


来源:https://stackoverflow.com/questions/23935793/matlabhow-to-interpolate-non-motonic-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!