简体   繁体   English

Matlab插值1D散乱数据

[英]Matlab Interpolating 1D Scattered Data

I have some data that is organized simply as 2D (x,y) coordinates. 我有一些数据简单地组织为2D(x,y)坐标。 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. 我有大量的这些数据,而X轴的数据对于所有点都有相同的范围,它不会在数据集之间使用相同的精确X点。 I would like to interpolate each set of data and then grab the same X points for each data set. 我想插入每组数据,然后为每个数据集获取相同的X点。 Whenever I use Matlab to try and interpolate the data I run into problems. 每当我使用Matlab尝试插入数据时,我都会遇到问题。

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]

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. 其中x和y是我记录的数据值,而xq是我想从插值数据中获得的新x点。

typically I use the interp1 command as such: 通常我使用interp1命令:

f = interp1(x,y,xq);

unfortunately this command produces the error: "The grid vectors are not strictly monotonic increasing." 不幸的是,这个命令产生错误:“网格向量不是严格单调增加的。” This happens if I use griddata or related commands. 如果我使用griddata或相关命令,就会发生这种情况。 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 av (value) matrix, which is more dimensions than I have. 所以我试图使用scatterInterpolant,但似乎这个函数似乎不适合这种类型的数据,因为它需要x,y和av(value)矩阵,这比我的维度更多。

I am at a loss on how to continue, advice, and suggestions would be greatly appreciated. 我不知道如何继续,建议和建议将不胜感激。

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

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 (ie x = [0 1 1 2 3]; ). 您得到的错误意味着您的实际x向量未按此处的示例进行排序,或者它包含重复值(即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) . 您可以使用issorted(x)命令测试第一种情况,使用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);

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. 您可以搜索重复项并将它们移动± eps ,将它们平均,或丢弃它们。

Without data that duplicates your error, though, we're all just kind of guessing. 但是,如果没有重复错误的数据,我们只是猜测。

Update: 更新:

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. 我很久以前写过这篇文章,几乎肯定不是最有效的方法,但它会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM