if statement didn't executed in a loop

Hi all, I have problem on the if statement that inside can't be executed which makes other operations in the code cannot be run. My code is as follow, the problems is occurred at the "if isequal(time(i),off_tt)" parts where the variables 'k' didn't increment.
k = 0; on_t = 2;off_t = 2;
for i=1:length(time)
on_tt = (on_t*(k+1))+(off_t*k)
off_tt = (on_t*(k+1))+(off_t*(k+1))
if(time(i) ~= off_tt)
if (on_t == 0)
ref_speed_left = [ref_speed_left, 0];
ref_speed_right = [ref_speed_right, 0];
elseif (off_t == 0)
ref_speed_left = [ref_speed_left, ref_speedR];
ref_speed_right = [ref_speed_right, ref_speedL];
elseif (time(i) < on_tt) %ans == 0
ref_speed_left = [ref_speed_left, ref_speedR];
ref_speed_right = [ref_speed_right, ref_speedL];
elseif (time(i)>= on_tt && time(i) < off_tt+1)
if isequal(time(i),off_tt) %problems at this if statement
k=k+1; %k didn't increment when it
end %met the requirement
ref_speed_left = [ref_speed_left, 0]
ref_speed_right = [ref_speed_right, 0]
end
end
end
May I know any solutions for this, your helps is much appreciate. Thanks you.

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
Can you post values of ref_speedR and others variables, to allow testing your code
Shawn Chang
Shawn Chang 2012년 12월 13일
편집: Shawn Chang 2012년 12월 13일
time = 0.05:0.05:14.95;
ref_speedR = 0.3595;
ref_speedL = 0.3693;

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

답변 (3개)

Ilham Hardy
Ilham Hardy 2012년 12월 13일

0 개 추천

for i=1:length(time)
k=i %correction
on_tt = (on_t*(k+1))+(off_t*k)
off_tt = (on_t*(k+1))+(off_t*(k+1))
You declare k=0, therefore k is 0.

댓글 수: 1

Shawn Chang
Shawn Chang 2012년 12월 13일
"k=0" is used to initialize the k.
When on_tt and off_tt value has been met and k will be increment by 1 to obtain a new on_tt and off_tt.
eg. last value in time(i) = 15, on_t = 2, off_t = 2;
When k = 0; on_tt = 2, off_tt = 4;
When k = 1; on_tt = 6, off_tt = 8;
until either on_tt or off_tt reached 15, it will break the for loop.
So, "k=i" cannot be used in this case

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

Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
편집: Azzi Abdelmalek 2012년 12월 13일

0 개 추천

Are you sur the condition if isequal(time(i),off_tt) is true? I tested your code, the condition is true only for i=80

댓글 수: 8

Shawn Chang
Shawn Chang 2012년 12월 13일
편집: Shawn Chang 2012년 12월 13일
I am not sure because logically "if isequal(time(i),off_tt)" is executed when time(i) is equal to off_tt and thus increment the variable, k but it didn't in this case.
eg. when time(80) = 4 and off_tt = 4, but it didn't executed the if statement so I am wonder what mistake I had done in my code.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
편집: Azzi Abdelmalek 2012년 12월 13일
Ok, when time(80) = 4, off_tt = 4, you forget that your code is executed when the condition if(time(i) ~= off_tt) is true, which it is not in this case.
Ok, but when I changed the code as follow, the code still not working.
k=0;on_t = 2;off_t = 2;
for i=1:length(time)
on_tt = (on_t*(k+1))+(off_t*k)
off_tt = (on_t*(k+1))+(off_t*(k+1))
if(time(i) ~= off_tt)
if (on_t == 0)
ref_speed_l = [ref_speed_l, 0];
ref_speed_r = [ref_speed_r, 0];
elseif (off_t == 0)
ref_speed_l = [ref_speed_l, ref_speedR];
ref_speed_r = [ref_speed_r, ref_speedL];
elseif (time(i) < on_tt) %ans == 0
ref_speed_l = [ref_speed_l, ref_speedR];
ref_speed_r = [ref_speed_r, ref_speedL];
elseif (time(i)>= on_tt && time(i) < off_tt+1)
ref_speed_l = [ref_speed_l, 0]
ref_speed_r = [ref_speed_r, 0]
end
end
if isequal(time(i),off_tt)
k=k+1;
end
end
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
In this case the condition isequal(time(i),off_tt) occurs one time, look at k, (k=1);
Shawn Chang
Shawn Chang 2012년 12월 13일
Yes, k is occurs one time. May I know how to refine the code so that can increment k further?
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
Do you want to increment k each increment of i, if not tell us what is the condition?
Shawn Chang
Shawn Chang 2012년 12월 13일
I want to increment k when the element of time(i) is equal to off_tt, so that a new on_tt and off_tt will be obtained and the process run until the for loop is break.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
you have then to revise your code

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

Roger Stafford
Roger Stafford 2012년 12월 13일

0 개 추천

Shawn, look at the third line from the beginning of the for-loop:
if(time(i) ~= off_tt)
It is an outer condition that must be satisfied if you are to enter the if-elseif-elseif-end sequence. This means you cannot reach the
if isequal(time(i),off_tt)
test with equal values of time(i) and off_tt. Hence the 'isequal' test will always be false and k can never be incremented.
I am curious as to why you used 'isequal' at that point. It is intended for testing equality between entire arrays and suggests that you may have meant something else in your code here.
Roger Stafford

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by