フィルターのクリア

Problem in executing a for loop

1 回表示 (過去 30 日間)
Sai
Sai 2015 年 9 月 16 日
コメント済み: Walter Roberson 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...
Walter Roberson
Walter Roberson 2015 年 9 月 16 日

サインインしてコメントする。

回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by