0

suppose I have the following matrix

a =

76 NaN 122 NaN

78 NaN 123 NaN

84 NaN 124 54

77 NaN 126 58

82 45 129 62

90 50 135 45

76 63 133 66

79 52 122 49

88 56 140 24

Is there any way to calculate exponential moving average for each column, disregarding the first NaN values? For instance, if I use a 3 days exponential factor, I would expect to get a matrix starting with 2 NaN values in the 1st column, 6 NaN values in the 2nd column,2 NaN values in the 3rd column and 4 NaN values in the 4th column. Any suggestion? Thank you in advance

mario
  • 75
  • 8

1 Answers1

1

Just use filter on the whole matrix, which will pass through the NaN's as appropriate. If you want to "infect" edge values with NaN as well, add some extras at the top edge, then trim the result:

kernel = [1 1 1].';   % Any 3-element kernel, as column vector
a2 = [repmat(NaN, 2, 4); a];  % Add extra NaN's at the start, to avoid partial answers
xtemp = filter(kernel, 1, a2);
x = xtemp(3:end, :);
Peter
  • 14,559
  • 35
  • 55
  • thanks!, I follow your answer, i get the Nans where I want them to be, but the final matrix returns sums of values of a. Is there a way I can calculate exponential moving average instead? – mario May 30 '12 at 13:55
  • I just used a sample filter kernel of values [1 1 1], which adds values. I intend that you replace that kernel with whatever 3-element filter kernel you want. I'm not sure how you define exponential moving average, so I didn't write out a kernel for you. – Peter May 30 '12 at 13:57