Loop is running only for certain value in the whole range
조회 수: 4 (최근 30일)
이전 댓글 표시
For a data set of 6000 values,a loop is being run to calculate certain value but it is showing value for only 60 elements. The excerpt from code is given as :
t=0:0.005:29.995; %time duration
v=importdata('BAG_20051214_071014.txt'); %input data file
z=v.^2;
l=6001;
ns=1;
h=(1/ns)*(t>=0);
nl=60;
g=(1/nl)*(t>=0);
i = nl+1;
while i<=l
for j = i-nl:i-1 % Loop to compute LTA
x(j)=z(i-j);
lta(j)=g(j).*x(j);
end
i=i+nl;
end
This part x(j)=z(i-j) is showing values only for 60 values but the total range of data is 6000. Kindly help.
댓글 수: 15
Rik
2019년 6월 22일
If you want to ask a new question, post it as a separate question, don't edit this one. Your last edit after Guillaume's comment seems like a valid question, but has nothing to do with the comments or the answer below.
채택된 답변
per isakson
2019년 6월 13일
편집: per isakson
2019년 6월 13일
I'm not a fan of backward engineering of faulty code. The formula of one of your comments is the formula of a moving average. Matlab has a moving average function, movmean. One could also use convolution or a filter.
Here is a code that somewhat adheres to your code. I might have missed the exact position of the window by one.
[x1,z] = cssm_();
whos x1 z
figure; plot([x1',z'],'.')
x2 = movmean( z, [0,59], 'Endpoints','discard' );
figure; plot([x1(61:end)',x2(2:end)'],'.')
function [ x ,z ] = cssm_()
%#ok<*AGROW>
v = importdata('BAG_20051214_071014.txt'); %input data file
z = reshape( v.^2, 1,[] );
len=length(v);
x = nan( 1, len );
nl = 60;
g = ones( nl, 1 ) / nl;
for jj = nl+1:len
x(jj) = z( jj-nl+1 : jj ) * g;
end
end
In response to comment
I still don't understanding what you mean by "code for x2", "theoretical values" and "match". Anyhow, the following check shows that the values of x1 and x2 agree (except for one value at the edge).
>> d = x1(61:end)-x2(2:end);
>> figure; plot(d,'.')
댓글 수: 3
per isakson
2019년 6월 13일
Answer by editing in comments is not a good idea!
See the addendum to my answer.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!