Matlab的一维插值散乱数据 [英] Matlab Interpolating 1D Scattered Data

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

问题描述

我有作为2D(X,Y)简单地组织一些数据坐标。我有大量这种数据并同时为X轴的数据具有用于所有点相同的范围内,它不使用的数据集之间的相同确切的x分。我想插每一组数据,然后抓住相同的X点,每个数据集。每当我用Matlab尝试和插值数据我遇到的问题。

I have some data that is organized simply as 2D (x,y) coordinates. I have a large amount of this data and while the data for the X axis has the same range for all the points, it doesn't use the same exact X points between data sets. I would like to interpolate each set of data and then grab the same X points for each data set. Whenever I use Matlab to try and interpolate the data I run into problems.

所以,我的数据是这样的:

So my data looks like:

x = [0 1 2 3 4 5 6 7]
y = [2.2 3.7 3.9 4.1 4.2 8.9 9.1 9.3]
xq = [0.5 0.75 2 2.25]

其中x和y是我记录的数据值和XQ是我想从插数据得到的。新的X点

where x and y are my recorded data values and xq are the new x points that I would like to get from the interpolated data.

通常我用interp1命令,例如:

typically I use the interp1 command as such:

f = interp1(x,y,xq);

可惜这个命令会产生错误:网格向量不严格单调递增。这种情况如果我使用的GridData或相关的命令。我明白,这是因为我不是从一个函数的数据,所以我需要一个技术,有散在的数据交易。因此,我已试图使用scatteredInterpolant但看来这功能似乎是不适合这种类型的数据,因为它需要的x,y,和一个V(值)矩阵,这是更尺寸比我

unfortunately this command produces the error: "The grid vectors are not strictly monotonic increasing." This happens if I use griddata or related commands. I understand that this is because I have data that isn't from a function and therefore I need a technique that deals with scattered data. So I have attempted to use scatteredInterpolant but it appears that this function appears to be not suited for this type of data, as it needs x, y, and a v (value) matrix, which is more dimensions than I have.

我就如何继续,建议,并建议将不胜AP preciated的损失。

I am at a loss on how to continue, advice, and suggestions would be greatly appreciated.

推荐答案

我要带刺这个,但你真的需要提供您code的实际工作示例和实际版本抛出错误的输入数据。在code和数据您发布的工作就好了:

I'm going to take a stab at this, although you really do need to provide a real working example of your code and an actual version of the input data that throws the error. The code and data you posted work just fine:

f =

         2.95        3.325          3.9         3.95

你得到的错误意味着实际 X 向量不排序,因为它是在你的例子在这里,否则它包含重复的值(即 X = [0 1 1 2 3]; )。您可以使用 issorted(X)命令测试首例和第二与任何(差异(X)== 0)

The error you're getting implies that your actual x vector is not sorted as it is in your example here, or else it contains duplicate values (i.e. x = [0 1 1 2 3];). You can test for the first case using the issorted(x) command and for the second with any(diff(x) == 0).

第一种情况很容易解决:

The first case is easy to fix:

[x,ix] = sort(x);
y = y(ix);
xq = sort(xq);
yq = interp1(x,y,xq);

有一对夫妇的方式来处理第二种情况,根据您的应用程序。您可以搜索重复和± EPS 转向他们,他们平均起来,还是放弃他们。

There are a couple ways to deal with the second case, depending on your application. You can either search for the duplicates and shift them by ±eps, average them together, or discard them.

如果没有复制你的错误数据,不过,我们都只是一种猜测。

Without data that duplicates your error, though, we're all just kind of guessing.

更新:

看着你贴(和对子孙后代的缘故,你应该直接编辑成你的问题),你有这两个问题的数据。您的数据不排序,并有重复。排序,正如我所说,是很容易。的按上述分类后的,这是我在过去所做的那样照顾重复条目:

Looking at the data you posted (and for posterity's sake you should edit directly into your question), you have both problems. Your data isn't sorted and there are duplicates. Sorting, as I said, is easy. After sorting as above, this is what I've done in the past to take care of duplicate entries:

xu = unique(x);
yu = y;
duplicated = xu(histc(x,xu) > 1); % find the duplicated entries
discardIndex = [];
for k = 1:length(duplicated);
    dupIndex = sort(find(x == duplicated(k))); % look for duplicated entries
    keepIndex = dupIndex(1); % keep only the first one
    discardIndex = [discardIndex dupIndex(2:end)]; % add the rest to a list
    yu(keepIndex) = mean(y(:,dupIndex)); % take the mean of the y values at the duplicated x values
end
yu(discardIndex) = []; % after all is said and done, delete the duplicated entries.

我写了很长一段时间以前,几乎可以肯定不是做最有效的方式,但它会奏效。

I wrote that a long time ago and is almost certainly not the most efficient way to do it, but it will work.

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

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