```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorising a simple loop
Date: Thu, 31 Mar 2011 20:19:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 19
Message-ID: <in2njo\$gif\$1@fred.mathworks.com>
References: <in1e65\$s00\$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1301602745 16975 172.30.248.46 (31 Mar 2011 20:19:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 31 Mar 2011 20:19:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:719396

"david " <davidtra84@gmail.com> wrote in message <in1e65\$s00\$1@fred.mathworks.com>...
> ......
> z is a single column vector ranging from -100 to +100 in a random order.
> r=70;
> y=45;
> x=[-abs(z>r) , abs(z<-r)];
> x=x(:,1)+x(:,2);
> for i=1:length(x)-1;
>              if       x(i)<0 && z(i+1)<y;
>                       x(i+1)=-1;
>              elseif  x(i)>0 && z(i+1)>-y;
>                      x(i+1)=1;
>              end
> end
> .......
- - - - - - - -
Are you sure that code does what you want?  At any given i you are affecting x(i+1) which will then be tested on the next step.  In other words the value of x at any given point can be affected by all previous values of z.  If you were to run the for-loop backwards with for i = length(x)-1:-1:1, you would presumably get a different answer.

Roger Stafford
```