Loop is running only for certain value in the whole range

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

Have you double checked that z is not empty except for those values?
Some side notes about your code:
i = nl+1;
This line is not necessary, as you immediately redefine i at the beginning of the for loop.
i=i+1;
Manually advancing a loop index is considered bad practice, and generally doesn't work. I strongly advise against using this in you code.
for j = i-nl:i-1 % Loop to compute LTA
x(j)=z(i-j);
lta(j) = g(j).*z(i-j); % Sum LTA window
end
Although this loop adjusts the index back to cover a set of 60 values, 59 of those values will be overwritten for the next i value. What is the point of running this loop? As an example, the first value of i is 61, so this loop will cover j values from 1 to 60. The second i value is 62, which means that the loop will cover values from 2 to 61.
Yes i have checked the z values,they are correct. But this loop is running for 60 values,i want to calculate the same things for 6000 values keeping the j limits same. Is there any other possible way to calculate values for 6000 datas keeping j limits same?
Looks like you forgot to attach 'BAG_20051214_071014.txt' with the paper clip icon, so I didn't bother delving into your code yet. It's so much faster to debug when you have the actual data than try try to guess without data.
What is t in your code?
Undefined function or variable 't'.
Error in Junk (line 8)
h= (1/ns)*(t>=0);
t is the time period.
I would like to know what you are trying to do? If this is known code can be simplified.
I am trying to calculate this formula in MATLAB.
while i<=l
for j = i-nl:i-1 % Loop to compute LTA
x(j)=z(i-j);
end
end
You do not modify i or l inside your while loop, so this is an infinite loop: if i<=l is true at the time the loop is encountered, the loop will run forever.
Sorry, I have updated that. Stil the same problem.
It looks to me as if you should be reshaping the z array to nl rows, and flipud() that, and assign that to x, possibly followed by a reshape to vector.
z(j) = v(i)^2 in the formula is that a typo?
@gulu,
I'm not sure what you're hoping to achieve by editing your question away, other than making it less likely that you'll receive help in the future. It's something we really don't like. It's also a complete waste of time since it will be restored by Mathworks.
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.
(Answers Dev) Restored edit

Sign in to comment.

 Accepted Answer

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

The summation is implemented by
z( jj-nl+1 : jj ) * g;
Is this homework?
What do you mean by "practical and theoretical values" ?
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

Asked:

on 12 Jun 2019

Commented:

on 8 Jul 2019

Community Treasure Hunt

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

Start Hunting!