Loop is running only for certain value in the whole range

2 views (last 30 days)
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 Comments
Rik
Rik on 22 Jun 2019
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.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 13 Jun 2019
Edited: per isakson on 13 Jun 2019
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,'.')
Capture.PNG
  3 Comments
per isakson
per isakson on 13 Jun 2019
What do you mean by "practical and theoretical values" ?
per isakson
per isakson on 13 Jun 2019
Answer by editing in comments is not a good idea!
See the addendum to my answer.

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!