From: us on
On Mar 29, 1:29 pm, "Selvaraaju " <mselvara...(a)gmail.com> wrote:
> us <u...(a)neurol.unizh.ch> wrote in message <2050b661-4658-4c0c-b9ca-da42c6887...(a)g28g2000yqh.googlegroups.com>...
> > On Mar 29, 1:09 pm, "Selvaraaju " <mselvara...(a)gmail.com> wrote:
> > > What is the easiest way to generate alternate +1 & -1 sequence?
>
> > > like if N= 5
>
> > > 1 -1 1 -1 1
>
> > > so on..
>
> > one of the many solutions
>
> >      nr=5;
> >      r=sign(round(rand(1,nr))-.5);
>
> > us
>
> How could you use rand function to generate a determinstic sequence?
>
> If N= 4 then 1 -1 1 -1
> If N= 3 then 1 -1 1
> if N= 10 then 1 -1 .... 1
>
> I hope you understood the problem
>
> Your code gives different results for execution- Hide quoted text -
>
> - Show quoted text -

well... i obviously COMPLETELY misunderstood your OP(!)...
sorry for the confusion...

one of the (other) solutions

r=sign(round(bitand(0:10,1)-.5))
% r = -1 1 -1 1 -1 1 -1 1 -1 1
-1

:-)
us
From: us on
us
> one of the (other) solutions
> r=sign(round(bitand(0:10,1)-.5))
> % r = -1 1 -1 1 -1 1 -1 1 -1 1 -1

-or- more simple, of course...

nr=6;
r=sign(bitand(0:nr,1)-.5)
% r = -1 1 -1 1 -1 1 -1

us
From: us on
"us "
> nr=6;
> r=sign(bitand(0:nr,1)-.5)
> % r = -1 1 -1 1 -1 1 -1

-or- a bit faster...

nr=6;
r=repmat([-1,1],1,fix(nr/2)); % <- in case NR is not EVEN...

us
From: Bjorn Gustavsson on
"us " <us(a)neurol.unizh.ch> wrote in message <hoq3mt$ce0$1(a)fred.mathworks.com>...
> us
> > one of the (other) solutions
> > r=sign(round(bitand(0:10,1)-.5))
> > % r = -1 1 -1 1 -1 1 -1 1 -1 1 -1
>
> -or- more simple, of course...
>
> nr=6;
> r=sign(bitand(0:nr,1)-.5)
> % r = -1 1 -1 1 -1 1 -1
>
> us
>
Simpler still(?)

r = (-1).^[0:n-1]

At least better golf?
Bjeorn
From: Greg Heath on
On Mar 29, 7:09 am, "Selvaraaju " <mselvara...(a)gmail.com> wrote:
> What is the easiest way to generate alternate +1 & -1 sequence?
>
> like if N= 5
>
> 1 -1 1 -1 1
>
> so on..

help repmat

Hope this helps.

Greg