Dynamic moving average with specific criteria

I have some code that takes a column vector and performs a moving average on these data. The results from the moving average are placed in a cell array. Then, the window size of the averaging function increases (window size is set by i=1:60; amd n=round(10.^(i/10)); ) and the moving average is repeated. The results are then placed in to the next column of the cell array...and the process repeated:
m=cell(1000001:60);
sd=zeros(1,60);
for i=1:60;
n=round(10.^(i/10));
mask=ones(n,1)/n;
a=conv(P,mask,'valid');
m{i}=a;
sd(i)=std(a);
end
The problem:
In the original data being analysed (i.e. P), all values are either positive or 0. My goal is to only perform the averaging function on windowed data that do not either begin or end with a zero. It's ok for the block of data to contain zeroes, its just I don't want the averaging function to return a value to the cell array if the block begins or ends with a 0. I'm pretty new to matlab, and can't see how I can tell the code this, so any suggestions would be greatly appreciated.

 Respuesta aceptada

Jan
Jan el 17 de En. de 2013
Editada: Jan el 20 de En. de 2013
1000001:60 is the vector of the numbers from 1000001 to 60 in steps of 1. Because 60 is smaller than 1000001, this is the scalar 1000001 only. Therefore m=cell(1000001:60) is a large {1000001 x 1000001} cell. What do you want instead?
You can remove the values, which belong to the zero margins, after the calulations:
P0 = P == 0;
ignore = and(P0(1:end - n + 1), P0(n:end)); % Perhaps (n-1) etc.
a(ignore) = [];
[EDITED] Doh! No, 1000001:60 is not 1000001, but the empty matrix. Therefore m is not large, but empty.

5 comentarios

Swisslog
Swisslog el 17 de En. de 2013
I think I see what you mean, the m=cell(1000001:60) line is my clumsy way of creating the cell array into which the results get placed.
The array is 60 cells wide, and the 1st cell has 1000001 values in it (as this first cell has the results of the moving average using a window of size 1). I realize now this is not good (but didn't notice because the code at least works...). I'm afraid I'm not clear however on how to clean this up?
To try and make it clearer, the output I ultimately want is 60 columns containing the moving average results from each window used (with of course the averages which are calculated from blocks of data begnning or ending with 0 removed or at least set to NaN)
Thanks for the help - I am though still having some issues with the line:
ignore = and(P0(1:end - n + 1), P0(n:end)); % Perhaps (n-1) etc.
It does not ignore the averaging windows beginning with or ending with 0. It only seems to ignore averages calculated in windows containing all 0s. If I just write:
ignore = P0(1:end - n + 1)
Then I can ignore all averages calculated in windows beginning with 0. I can't quite seem to get the code right to ignore all those averages whose windows end in a zero though :-/ Any help would be much appreciated
Jan
Jan el 20 de En. de 2013
Editada: Jan el 20 de En. de 2013
For the pre-allocation of the output cell m you need:
m = cell(60, 1);
Please post some example data with e.g. 12 values and the wanted result for a specific window size.
Cedric
Cedric el 21 de En. de 2013
Editada: Cedric el 21 de En. de 2013
I think that what Jan had in mind is that:
P = [1 2 0 3 4 5 0 6 7] ;
n = 2 ; % 1,..,numel(P)
mask = ones(n, 1) / n ;
a = conv(P, mask, 'valid') ;
P0 = P == 0 ;
ii = find(P0)-n+1 ;
ii = ii(ii>0) ;
ignore = P0(1:end-n+1) ;
ignore(ii) = true ;
a(~ignore) % in your loop, m{i} = a(~ignore) ;
Swisslog
Swisslog el 21 de En. de 2013
Thank you Jan and Cedric, this is a great help. Looking at it now I can get my desired output by simply changing the AND in Jan's code to OR I believe.
Hence, if we have P=[0 0 2 2 1 0 2] for a window = 1, answer = [2 2 1 2] window = 2, answer = [2 1.5] window = 3, answer = [1.6667 1] window = 5, answer = [1.4]
etc...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de En. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by