Filter löschen
Filter löschen

Dynamic moving average with specific criteria

3 Ansichten (letzte 30 Tage)
Swisslog
Swisslog am 17 Jan. 2013
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.

Akzeptierte Antwort

Jan
Jan am 17 Jan. 2013
Bearbeitet: Jan am 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 Kommentare
Cedric
Cedric am 21 Jan. 2013
Bearbeitet: Cedric am 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 am 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...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by