Filter löschen
Filter löschen

Problem in executing a for loop

1 Ansicht (letzte 30 Tage)
Sai
Sai am 16 Sep. 2015
Kommentiert: Walter Roberson am 16 Sep. 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 Kommentare
Sai
Sai am 16 Sep. 2015
Bearbeitet: Walter Roberson am 16 Sep. 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...

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 16 Sep. 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 Kommentar
Sai
Sai am 16 Sep. 2015
Bearbeitet: Sai am 16 Sep. 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

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 16 Sep. 2015
Bearbeitet: Guillaume am 16 Sep. 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 Kommentar
Sai
Sai am 16 Sep. 2015
Bearbeitet: Sai am 16 Sep. 2015
Thanks for your answer, I've not tried debugger before. I'll try now :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by