필터 지우기
필터 지우기

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...

댓글을 달려면 로그인하십시오.

답변 (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