clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:10
while a~=length(vec)+1
break
if vec(a)==0 & vec(a+1)~=0
d=d+1;
indext(d)=a;
end
end
end
the indext vector doses not create in Matlab workspace.
why?
of course I know by chaging the code to :
clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:9
if vec(a)==0 & vec(a+1)~=0
d=d+1;
indext(d)=a;
end
end
the indext vector will create in Matlab workspace.
but i want this vector be create by using while and breake
command.
how sjould i change my source code?by using while.
thanks.
Den Wed, 26 Sep 2007 14:40:59 +0000 skrev ehsan mirrahimi:
> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?
>
> of course I know by chaging the code to :
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:9
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
>
>
> the indext vector will create in Matlab workspace.
> but i want this vector be create by using while and breake
> command.
>
> how sjould i change my source code?by using while.
> thanks.
Why not remove the for, whiles etc and replace it with
PB <pbodin@_REMOVE_THiS_kth.se> wrote in message
<A1wKi.9762$ZA.5842@newsb.telia.net>...
> Den Wed, 26 Sep 2007 14:40:59 +0000 skrev ehsan mirrahimi:
>
> > please run this code:
> >
> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:10
> > while a~=length(vec)+1
> > break
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> > end
> >
> >
> > the indext vector doses not create in Matlab workspace.
> > why?
> >
> > of course I know by chaging the code to :
> >
> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:9
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> >
> >
> > the indext vector will create in Matlab workspace.
> > but i want this vector be create by using while and breake
> > command.
> >
> > how sjould i change my source code?by using while.
> > thanks.
>
> Why not remove the for, whiles etc and replace it with
>
> d=find(vec)-1
>
> or am I missing something here?
>
> /PB
Prob what PB said. To answer your question let's properly
format your code
> > clear;
> > d=0;
> > vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> > for a=1:10
> > while a~=length(vec)+1
> > break
> > if vec(a)==0 & vec(a+1)~=0
> > d=d+1;
> > indext(d)=a;
> > end
> > end
> > end
So the first time the while statement is satisfied it breaks
out of the loop. Thus your loop SHOULD do nothing as coded.
"ehsan mirrahimi" <ehsan.mirrahimi@mathworks.com> wrote in
message <fddr1q$svn$1@fred.mathworks.com>...
> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?
because your code is illogical.
look at this line by line...
> for a=1:10
this is ok, loop for a=1:10
> while a~=length(vec)+1
ok, you will get into the while if a~=length(vec)+1
length(vec)+1 looks like 11 to me, since a=1:10 then you
will always go into the while. so it really doesn't do
anything for you here.
> break
this is the real problem. as soon as you get into the
while loop you break... none of the rest of the code will
be executed between the break and the end of the loop.
what did you think the break was going to do at this
point? There is no use continuing since the code is never
executed.
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
this is a good example to learn how to use the debugger
on. you could step through this and immediately see why
you never got to what you wanted to do.
The break statement is performing just fine. In your for
loop, 'a' goes from 1 to 10, so it is never be equal to
length(vec)+1 = 11.
Thus for every 'a' value the while loop will execute and
immediately break, which puts you back in the for loop. So
with that break statement where it is you are basically
doing just this:
clear;
d=0;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
for a=1:10
end
> please run this code:
>
> clear;
> d=0;
> vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
> for a=1:10
> while a~=length(vec)+1
> break
> if vec(a)==0 & vec(a+1)~=0
> d=d+1;
> indext(d)=a;
> end
> end
> end
>
>
> the indext vector doses not create in Matlab workspace.
> why?
My crystal ball says:
for a=1:10
if a == 10
continue;
end
if vec(a) == 0 % snip rest of your code
end
end
If you really must use a while and a break, which is not at
all the best way to do this, here is one way. Note this
assumes the output from your second code is the one you want!
clear;
d=1;
vec=zeros(1,10);vec(2)=3;vec(5)=1;vec(8)=4;
while true
if vec(d)==0 & vec(d+1)~=0
indext(d) = d;
end
d = d+1;
if d+1 > length(vec)
break
end
end
indext = indext(find(indext));
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.