Azzera filtri
Azzera filtri

Problem in executing a for loop

1 visualizzazione (ultimi 30 giorni)
Sai
Sai il 16 Set 2015
Commentato: Walter Roberson il 16 Set 2015
I'm trying to filter data for Quadrant hole analysis. My intention is to calculate total sum (S_Q1_H) for each H-value which I'm trying to implement via a for loop initiated at the beginning as for H=1:4 Even though the code is being executed, the output shows that all the 4 values of total sum S_Q1_H are same
No matter how many times I check, I'm unable to find where the logic error is... Here is the code.
for H=1:4
temp=0;
for n = 1:1000000
sprintf('%f',H);
if and(u_real(n)>0,v_real(n)>0)
if n == temp + 1
u_Q1(n) = u_real(n);
v_Q1(n) = v_real(n);
if ((u_Q1(n)*v_Q1(n))> (H*s_U*s_V))
u_Q1_H(n)= u_Q1(n);
v_Q1_H(n)= v_Q1(n);
end
temp = n;
elseif n~= temp+1
d = n-temp;
u_Q1(n) = u_real(n);
v_Q1(n) = v_real(n);
if ((u_Q1(n)*v_Q1(n))> (H*s_U*s_V))
u_Q1_H(n)= u_Q1(n);
v_Q1_H(n)= v_Q1(n);
end
temp = n-d+1;
end
end
end
u_Q1_H_s = u_Q1_H(u_Q1_H~=0);
v_Q1_H_s = v_Q1_H(v_Q1_H~=0);
[f,s1]=size(u_Q1_H_s);
temp=0;
for o=1:s1
S_Q1_H(H) = temp + u_Q1_H_s(o)*v_Q1_H_s(o);
temp = S_Q1_H(H);
S_avg_Q1(H) = temp/o;
end
end
  2 Commenti
Sai
Sai il 16 Set 2015
Modificato: Walter Roberson il 16 Set 2015
Can anyone tell me the syntax to create a new variable in each iteration of for loop
for H=1:4
if ((u_Q1(n)*v_Q1(n))> (H*s_U*s_V))
u_Q1_H'num2str(H)'(n)= u_Q1(n);
v_Q1_H'num2str(H)'(n)= v_Q1(n);
end
end
My aim is to create u_Q1_H1(n) for H=1, u_Q1_H2(n) for H=2...and so on...

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 16 Set 2015
Inside your "for n" loop you have
if n == temp + 1
but you have not initialized temp in the given code before you use it here.
If you do happen to have an initialized temp before starting the loop, then note that the next H along, the temp value will be temp = S_Q1_H(H); from the "for o" loop. It seems unlike that is what you want.
  1 Commento
Sai
Sai il 16 Set 2015
Modificato: Sai il 16 Set 2015
Thanks for your answer, Yes, I do have a 'temp' initialized before that..and after the 'for o' loop But, I still observe the output is same for all the 4 values. After the first iteration it, seems the 'for n' loop isn't executing. Struggling to find the answer

Accedi per commentare.


Guillaume
Guillaume il 16 Set 2015
Modificato: Guillaume il 16 Set 2015
The best way for you to understand why your program is not giving you the correct result is to use the debugger. Place a breakpoint on the first line of code, step through each line and observe the result of each operation as it is executed. You'll quickly see if your loop is executed and if not, why not.
  1 Commento
Sai
Sai il 16 Set 2015
Modificato: Sai il 16 Set 2015
Thanks for your answer, I've not tried debugger before. I'll try now :)

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by