# how i can perform this for loop

1 view (last 30 days)
Tammun filistin on 22 Jun 2018
Edited: Tammun filistin on 22 Jun 2018
Hello im trying to perform this cod 20 times but the for loop moves one time"j=1 instead of 20" could anyone please help me to solve it!
clear all
close all
clc
for j=1:20
c=[1 2 3 5 6 7 8 99 9 3 4 55 66 7 8 9 9 97 0 1 2 ];
for k=1:length(c)
if c(k)>=5
ag(k)=1
k1=k
return
elseif c(k)<=-1
ag(k)=0
k1=k;
return
else
end
end
m=k1
l(j)=sum(ag)
end
j
##### 2 CommentsShowHide 1 older comment
Tammun filistin on 22 Jun 2018
Edited: Tammun filistin on 22 Jun 2018
actually my victor c contain random value in each j round and I need the inner loop to check the value that satisfy the if condition in each round.. (Im looking for l(j)=sum(ag)). the resulting l vector will give different value when c vector is changing

Pieter Hamming on 22 Jun 2018
Replace "return" by "continue". "Return" stops the whole file, "continue" sends it on to the next iteration in the for loop.
Tammun filistin on 22 Jun 2018
Edited: Tammun filistin on 22 Jun 2018
My c vector is changing in every j round and my aim is to see when if condition is satisfied (I mean in each j round I need to find the l(j)=sum(ag) sum(ag) which is changing by changing the vector c).
Thank you

You're using the return function.
This exits out of the j loop. Try instead replacing return with continue. I'm able to get j to iterate to 20 with that small change.
clear all
close all
clc
for j=1:20
c=[1 2 3 5 6 7 8 99 9 3 4 55 66 7 8 9 9 97 0 1 2 ];
for k=1:length(c)
if c(k)>=5
ag(k)=1
k1=k
continue
elseif c(k)<=-1
ag(k)=0
k1=k;
continue
else
end
end
m=k1
l(j)=sum(ag)
end
j
%% 20th iter
ag =
0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1
k1 =
18
m =
18
l =
13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13
j =
20
Tammun filistin on 22 Jun 2018
Edited: Tammun filistin on 22 Jun 2018
Dear sir Is theirs away to stop the the inner for loop (with counter k) when it satisfy the if condition then repeat the outer loop j times. If the c vector is changing in every j round the sum(ag) will change and this is my aim(Im traing to fined l(j)=sum(ag) when c is changing in every j round).
Thank you