Loop is running only for certain value in the whole range
Show older comments
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
Bob Thompson
on 12 Jun 2019
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.
Image Analyst
on 13 Jun 2019
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.
gulu
on 13 Jun 2019
KSSV
on 13 Jun 2019
What is t in your code?
Undefined function or variable 't'.
Error in Junk (line 8)
h= (1/ns)*(t>=0);
gulu
on 13 Jun 2019
gulu
on 13 Jun 2019
Walter Roberson
on 13 Jun 2019
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.
gulu
on 13 Jun 2019
Walter Roberson
on 13 Jun 2019
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.
per isakson
on 13 Jun 2019
z(j) = v(i)^2 in the formula is that a typo?
Guillaume
on 21 Jun 2019
@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.
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.
Rena Berman
on 8 Jul 2019
(Answers Dev) Restored edit
Accepted Answer
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!