set movmean values to NaN if large number of original data points in window are missing

Viewed 75

I have a 1-d data file with occasional NaN values. If I apply movmean to this input data, is there a simple way to set the movmean value to NaN if the number of input values within the moving window is greater than a threshold value? For example, if the window length is 10 and a threshold value is 3, I would like the movmean value to be NaN for this set of 10 values:

[1 3 NaN 4 NaN 2 5 NaN NaN 3] 

but the give me a valid movmean value for this set of 10 values:

[1 3 2 4 NaN NaN 3 2 5 3]
1 Answers

This is a matlab question, and you can do something like the following:

w = 10; t = 3;
A = [1 3 NaN 4 NaN 2 5 NaN NaN 3];
M = movmean(A,w,'omitnan');
N = movsum(isnan(A),w) >= t;
M(N) = NaN;
Related