Thread Subject: loop

Subject: loop

From: astro mmi

Date: 21 Nov, 2009 21:02:05

Message: 1 of 2

%perceptron to emulate AND function
%Madhumitha Iyer
x=[0 0 0 1;0 0 1 1;0 1 0 1;0 1 1 1;1 0 0 1;1 0 1 1;1 1 0 1;1 1 1 1];
t=[-1 -1 -1 -1 -1 -1 -1 1];
w=[0 0 0 0];
wnew=[0 0 0 0];
theta=0.2;
alpha=1;
delw=[0 0 0 0];
yt=[0;0;0;0;0;0;0;0];
%yarray=[0;0;0;0;0;0;0;0];
y=[0;0;0;0;0;0;0;0];
wc=0;
count=0;
totalwc=1;
wcarray=[0;0;0;0;0;0;0;0];
while(totalwc~=0)
totalwc=0;
for i=1:8
y=x*w';
if y(i,1)>theta
yt(i,1)=1;
elseif y(i,1)<-theta
yt(i,1)=-1;
else
yt(i,1)=0;
end
if yt(i,1)==t(1,i)
wnew=w;
else
wnew=w+alpha*t*x;
end
delw=wnew-w;
wc=sum(abs(delw));
w=wnew;
wcarray(i,1)=wc;
end
totalwc=totalwc+sum(wcarray);
totalwc
count=count+1
end

Hi everyone,
  The above code is for implementing a perceptron. The program gets struck in an infinite loop when I execute it.I am noticing this is happening because the totalwc variable is not changing at all and is remaining a constant at 12 which is what is making the loop to run indefinitely.Pls help me rectify the code so that i may get the right answer.
I also want to know how a goto or do-while can be implemented in matlab
Thanx.

Subject: loop

From: Matt

Date: 21 Nov, 2009 22:38:01

Message: 2 of 2

"astro mmi" <pyarsa_madhu@yahoo.co.in> wrote in message <he9kgd$qr3$1@fred.mathworks.com>...
> %perceptron to emulate AND function
> %Madhumitha Iyer
> x=[0 0 0 1;0 0 1 1;0 1 0 1;0 1 1 1;1 0 0 1;1 0 1 1;1 1 0 1;1 1 1 1];
> t=[-1 -1 -1 -1 -1 -1 -1 1];
> w=[0 0 0 0];
> wnew=[0 0 0 0];
> theta=0.2;
> alpha=1;
> delw=[0 0 0 0];
> yt=[0;0;0;0;0;0;0;0];
> %yarray=[0;0;0;0;0;0;0;0];
> y=[0;0;0;0;0;0;0;0];
> wc=0;
> count=0;
> totalwc=1;
> wcarray=[0;0;0;0;0;0;0;0];
> while(totalwc~=0)
> totalwc=0;
> for i=1:8
> y=x*w';
> if y(i,1)>theta
> yt(i,1)=1;
> elseif y(i,1)<-theta
> yt(i,1)=-1;
> else
> yt(i,1)=0;
> end
> if yt(i,1)==t(1,i)
> wnew=w;
> else
> wnew=w+alpha*t*x;
> end
> delw=wnew-w;
> wc=sum(abs(delw));
> w=wnew;
> wcarray(i,1)=wc;
> end
> totalwc=totalwc+sum(wcarray);
> totalwc
> count=count+1
> end
>
> Hi everyone,
> The above code is for implementing a perceptron. The program gets struck in an infinite loop when I execute it.I am noticing this is happening because the totalwc variable is not changing at all and is remaining a constant at 12 which is what is making the loop to run indefinitely.Pls help me rectify the code so that i may get the right answer.

==================

At the bottom of the loop, insert a breakpoint or a keyboard statement, e.g.,

count=count+1, keyboard

This will stop execution of the code after each iteration of the loop so that you can inspect all the variables and determine if they're evolving as you expect.



> I also want to know how a goto or do-while can be implemented in matlab

goto is not available in MATLAB. Coding using goto is also unnecessary and emphatically discouraged.

do-while can be implemented using a while structure as follows:

FirstIter=true;
while condition|FirstIter

  %blablabla

 FirstIter=false;
end

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
loop astro mmi 21 Nov, 2009 16:04:09
goto astro mmi 21 Nov, 2009 16:04:09
dowhile astro mmi 21 Nov, 2009 16:04:09
rssFeed for this Thread

Contact us at files@mathworks.com