Dynamic moving average with specific criteria

조회 수: 2 (최근 30일)
Swisslog
Swisslog 2013년 1월 17일
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.

채택된 답변

Jan
Jan 2013년 1월 17일
편집: Jan 2013년 1월 20일
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
Cedric
Cedric 2013년 1월 21일
편집: Cedric 2013년 1월 21일
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 2013년 1월 21일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by