Problem in executing a for loop

1 次查看(过去 30 天)
Sai
Sai 2015-9-16
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 个评论
Sai
Sai 2015-9-16
编辑:Walter Roberson 2015-9-16
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...

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2015-9-16
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 个评论
Sai
Sai 2015-9-16
编辑:Sai 2015-9-16
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

请先登录,再进行评论。


Guillaume
Guillaume 2015-9-16
编辑:Guillaume 2015-9-16
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 个评论
Sai
Sai 2015-9-16
编辑:Sai 2015-9-16
Thanks for your answer, I've not tried debugger before. I'll try now :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by