如何在Matlab中仅插入少于3个连续的Nan值?

时间:2016-06-19 16:10:24

标签: matlab interpolation nan

我有一个包含一些NaN值的500x600矩阵。我想插入少于三个NaN的位置(可能是前面的,后面的值的平均值),并且对于有超过3个连续NaN值的所有其他地方,我想将它们留作Nan值。我已经看过http://uk.mathworks.com/matlabcentral/answers/34481-interpolate-nans-only-if-less-than-4-consecutive-nans,但即使是接受的答案也行不通。 (我意识到这个是连续4个值,但无论如何都不起作用。)

1 个答案:

答案 0 :(得分:0)

如果通过编写3个连续的nans表示行或列中有3个连续的nans,则可以使用以下方法:

  1. 对于每一行,使用卷积来确定每个nans序列是否短于3。
  2. 使用following approach填充矩阵中的每一行。
  3. 通过转置结果并再次执行该功能来填充列。
  4. 代码:

    %generates example array
    data = rand(5,5); 
    data (1,2:4) = nan;
    data (2:5,2) = nan;
    data (:,4) = nan;
    
    %fills all relevan nans in a row
    data2 = interpolateNanRows(data );
    %fills all relevant nans in a column
    out= interpolateNanRows(data2')';
    

    辅助功能:

    function res = interpolateNanRows(data)
    %zero padding
    dataPad = zeros(size(data,1)+2,size(data,2)+2);
    dataPad(2:end-1,2:end-1)=data;
    
    %generates relevant nan maps
    nansMap = isnan(dataPad);
    irrelevantNans = conv2(double(nansMap),[1,0,0,0,1],'same')>0 & nansMap;
    
    %fills each row
    for ii=1:size(dataPad,1)
        filledRow = interpolateRow(dataPad(ii,:));
        %ignores irrelevant values (more than 3 consecutive nans)
        filledRow(irrelevantNans(ii,:)) = nan;
        dataPad(ii,:) = filledRow;
    end
    %generates output
    res  = dataPad(2:end-1,2:end-1);
    end
    
    
    function filledRow = interpolateRow(row)
    %receives a vector of values, and perform interpolation in regions of nans
    if sum(isnan(row))==0 || sum(isnan(row))==length(row)
        filledRow = row;
        return;
    end
    nanData = isnan(row);
    index   = 1:numel(row);
    filledRow   = row;
    filledRow(nanData) = interp1(index(~nanData), row(~nanData), index(nanData));
    
    end
    

    结果:

    data2=
    0.6386       NaN       NaN       NaN    0.6671
    0.4805       NaN    0.3171       NaN    0.7771
    0.1184       NaN    0.0124       NaN    0.6860
    0.2455       NaN    0.3011       NaN    0.8014
    0.7761       NaN    0.7239       NaN    0.2833
    
    
    out =
    
    0.6386    0.6457    0.6528    0.6599    0.6671
    0.4805    0.3988    0.3171    0.5471    0.7771
    0.1184    0.0654    0.0124    0.3492    0.6860
    0.2455    0.2733    0.3011    0.5512    0.8014
    0.7761    0.7500    0.7239    0.5036    0.2833