Loop is running only for certain value in the whole range

5 vues (au cours des 30 derniers jours)
gulu
gulu le 12 Juin 2019
Commenté : Rena Berman le 8 Juil 2019
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 commentaires
Rik
Rik le 22 Juin 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
Rena Berman le 8 Juil 2019
(Answers Dev) Restored edit

Connectez-vous pour commenter.

Réponse acceptée

per isakson
per isakson le 13 Juin 2019
Modifié(e) : per isakson le 13 Juin 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 commentaires
per isakson
per isakson le 13 Juin 2019
What do you mean by "practical and theoretical values" ?
per isakson
per isakson le 13 Juin 2019
Answer by editing in comments is not a good idea!
See the addendum to my answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by