SIMPLIFY FOR LOOP - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# SIMPLIFY FOR LOOP

Posts: 2Member
HELOO, I TRIED TO SIMPLIFY FOLLOWING:

sp=1:4;
ep=1:4;
for k=1:length(m4)
if m4(k)>0
return
end
sp(1)=k;
end
for k=sp(1)+1:length(m4)
if m4(k)==0
return
end
ep(1)=k+1;
end
for k=ep(1)+1:length(m4)
if m4(k)>0
return
end
sp(2)=k;
end
for k=sp(2)+1:length(m4)
if m4(k)==0
return
end
ep(2)=k+1;
end
for k=ep(2)+1:length(m4)
if m4(k)>0
return
end
sp(3)=k;
end
for k=sp(3)+1:length(m4)
if m4(k)==0
return
end
ep(3)=k+1;
end
for k=ep(3)+1:length(m4)
if m4(k)>0
return
end
sp(4)=k;
end
for k=sp(4)+1:length(m4)
if m4(k)==0
return
end
ep(4)=k+1;
end

!!!!TO THIS:
sp=1:4;
ep=1:4;
for k=1:length(m4)
if m4(k)>0
return
end
sp(1)=k;
end
for k=sp(1)+1:length(m4)
if m4(k)==0
return
end
ep(1)=k+1;
end
for j=2:4
for k=ep(j-1)+1:length(m4)
if m4(k)>0
return
end
sp(j)=k;
end

for l=sp(j)+1:length(m4)
if m4(l)==0
return
end
ep(j)=l+1;
end
end
BUT IT DID NOT WORK. CAN ANYONE TELL WHERE I AM MAKING MISTAKE????
THANKS