2

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]
pppery
  • 3,731
  • 22
  • 33
  • 46

1 Answers1

3

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;
gunes
  • 288
  • 3
  • 11
  • Yes, this is a Matlab question. However, in my case the vector A may have 1000 samples, of which the 10 numbers given are only one example window. I don't see how then to make the line N = movsum(isnan(A),w) >= t; be in synch with the corresponding movmean window. It is surprising that movmean does not have an option to require a percentage of the data to not be NaNs in order to give a valid mean. –  Feb 26 '22 at 18:57
  • movsum and movmean will use the same sliding windows. N will contain the number of NaNs for each window and we set the corresponding elements to NaN in the last line. If there is an error in the script, it should be trivial to fix. Would you like to try the above with some examples that you know the true answer to so that I do not misunderstand you? – gunes Feb 26 '22 at 20:31