MATLAB:使用插值替换缺失值(NaN) [英] MATLAB: Using interpolation to replace missing values (NaN)

查看:1794
本文介绍了MATLAB:使用插值替换缺失值(NaN)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元格数组,每个单元格包含一个值序列作为行向量.序列包含一些由NaN表示的缺失值.

I have cell array each containing a sequence of values as a row vector. The sequences contain some missing values represented by NaN.

我想使用某种插值方法替换所有NaN,如何在MATLAB中做到这一点?我也愿意就如何处理这些缺失的价值观提出其他建议.

I would like to replace all NaNs using some sort of interpolation method, how can I can do this in MATLAB? I am also open to other suggestions on how to deal with these missing values.

请考虑以下示例数据以说明问题:

Consider this sample data to illustrate the problem:

seq = {randn(1,10); randn(1,7); randn(1,8)};
for i=1:numel(seq)
    %# simulate some missing values
    ind = rand( size(seq{i}) ) < 0.2;
    seq{i}(ind) = nan;
end

结果序列:

seq{1}
ans =
     -0.50782     -0.32058          NaN      -3.0292     -0.45701       1.2424          NaN      0.93373          NaN    -0.029006
seq{2}
ans =
      0.18245      -1.5651    -0.084539       1.6039     0.098348     0.041374     -0.73417
seq{3}
ans =
          NaN          NaN      0.42639     -0.37281     -0.23645       2.0237      -2.2584       2.2294



根据回答,我认为这是一个困惑:显然我不是在处理随机数据,上面显示的代码只是数据结构的一个示例.

Based on the responses, I think there's been a confusion: obviously I'm not working with random data, the code shown above is simply an example of how the data is structured.

实际数据是某种形式的已处理信号.问题是在分析过程中,如果序列包含缺失值,那么我的解决方案将失败,因此需要过滤/插值(我已经考虑过使用每个序列的均值来填补空白,但我希望有更强大的功能)

The actual data is some form of processed signals. The problem is that during the analysis, my solution would fail if the sequences contain missing values, hence the need for filtering/interpolation (I already considered using the mean of each sequence to fill the blanks, but I am hoping for something more powerful)

推荐答案

好吧,如果要处理时间序列数据,则可以使用Matlab的内置插值函数.

Well, if you're working with time-series data then you can use Matlab's built in interpolation function.

类似的事情应该适合您的情况,但是您需要对其进行一些调整...即.如果您没有等间隔的采样,则需要修改times行.

Something like this should work for your situation, but you'll need to tailor it a little ... ie. if you don't have equal spaced sampling you'll need to modify the times line.

nseq = cell(size(seq))
for i = 1:numel(seq)
    times = 1:length(seq{i});
    mask =  ~isnan(seq{i});
    nseq{i} = seq{i};
    nseq{i}(~mask) = interp1(times(mask), seq{i}(mask), times(~mask));

end

您需要使用interp1的选项来弄清楚哪个选项最适合您的情况.

You'll need to play around with the options of interp1 to figure out which ones work best for your situation.

这篇关于MATLAB:使用插值替换缺失值(NaN)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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