Clear Filters
Clear Filters

Dynamic moving average with specific criteria

3 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 17 Jan 2013
Edited: Jan on 20 Jan 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 Comments
Cedric
Cedric on 21 Jan 2013
Edited: Cedric on 21 Jan 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 on 21 Jan 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...

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!