用Matlab进行插值 [英] Interpolation with matlab

查看:89
本文介绍了用Matlab进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有不同值的向量. 一些值是零,有时它们甚至一个接一个地出现. 我需要针对另一个具有相同大小的向量绘制该向量,但不能包含零. 我可以对向量进行某种插值的最佳方法是什么,怎么做? 我尝试阅读有关mat-lab的插值的知识,但我对实现插值的理解不够好. 如果有可能逐步向我解释它,我将不胜感激,因为我是这个程序的新手.

I have a vector with different values. Some of the values are zeros and sometimes they even come one after another. I need to plot this vector against another vector with the same size but I can't have zeros in it. What is the best way I can do some kind of interpolation to my vector and how do I do it? I tried to read about interpolation in mat-lab but I didn't understand good enough to implement it. If it's possible to explain it to me step by step I will be grateful since I'm new with this program.

谢谢

推荐答案

从包含两个等长向量xy的数据集开始,其中y等于零的值将被排除,优先选择不包含零的子集:

Starting from a dataset consisting of two equal length vectors x,y, where y values equal to zero are to be excluded, first pick the subset excluding zeros:

incld = y~=0;

然后您对该子集进行插值:

Then you interpolate over that subset:

yn = interp1(x(incld),y(incld),x);

示例结果,将x相对于y(绿色)绘制,将x相对于yn(红色)绘制:

Example result, plotting x against y (green) and x against yn (red):

编辑

请注意,根据插值的定义,如果终点为零,则必须分别进行处理,例如,通过在上一行之前运行以下内容:

Notice that, by the definition of interpolation, if terminal points are zero, you will have to take care of that separately, for instance by running the following before the lines above:

if y(1)==0, y(1) = y(find(y~=0,1,'first'))/2; end
if y(end)==0, y(end) = y(find(y~=0,1,'last'))/2; end

编辑#2

这是上面的2D版本,其中数组XY是与2D数组Z中的条目相对应的坐标:

And this is the 2D version of the above, where arrays X and Y are coordinates corresponding to the entries in 2D array Z:

[nr nc]=size(Z);
[X Y] = meshgrid([1:nc],[1:nr]);
X2 = X;
Y2 = Y;
Z2 = Z;
excld = Z==0;
X2(excld) = [];
Y2(excld) = [];
Z2(excld) = [];
ZN = griddata(X2,Y2,Z2,X,Y);

ZN包含插值点.

在下图中,深蓝色补丁显示零.左边在插值之前,右边在插值之后:

In the figure below, zeros are shown by dark blue patches. Left is before interpolation, right is after:

这篇关于用Matlab进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆