f

#### -1, +1

```Hi,

How can I  generate a random number as

-1 or +1 in a loop

TIA

``` 0 11/2/2003 7:04:11 PM comp.soft-sys.matlab  211265 articles. 25 followers. 4 Replies 891 Views Similar Articles

[PageSpeed] 19

```Try,

A=rand(5,5);
A(A>0.5)=1;
A(A<=0.5)=-1;

This makes a matrix of random +1/-1 you can probably get what you
need from this...

Volvo wrote:
>
>
> Hi,
>
> How can I generate a random number as
>
> -1 or +1 in a loop
>
> TIA
>
>
>
``` 0 11/2/2003 7:03:30 PM
```On Sun, 02 Nov 2003 14:03:30 -0500, Dan Ennis wrote:

> Try,
>
>
> A=rand(5,5);
> A(A>0.5)=1;
> A(A<=0.5)=-1;

A shorter version would be

A=sign(2*rand(5)-1);
A(A==0)=1;	% or however you want to assign it.

Of course this doesn't answer the OP's original homework problem of how to
do it in a loop.  I'll leave that as an exercise for the reader.

Dan

>
>
> This makes a matrix of random +1/-1 you can probably get what you
> need from this...
>
>
> Volvo wrote:
>>
>>
>> Hi,
>>
>> How can I generate a random number as
>>
>> -1 or +1 in a loop
>>
>> TIA
>>
>>
>>

``` 0 11/2/2003 7:20:36 PM
```well... it is not a homewrok but I did it in the same way of the first

:))

thanks...

``` 0 11/2/2003 10:58:36 PM
```"Dan Hensley" <danDOThensley@attbi.com> wrote in message
news:pan.2003.11.02.19.18.34.367314@attbi.com...
> On Sun, 02 Nov 2003 14:03:30 -0500, Dan Ennis wrote:
>
> > Try,
> >
> >
> > A=rand(5,5);
> > A(A>0.5)=1;
> > A(A<=0.5)=-1;
>
> A shorter version would be
>
> A=sign(2*rand(5)-1);
> A(A==0)=1; % or however you want to assign it.
>
> Of course this doesn't answer the OP's original homework problem of how to
> do it in a loop.  I'll leave that as an exercise for the reader.
>
> Dan
>
> >
> >
> > This makes a matrix of random +1/-1 you can probably get what you
> > need from this...
> >
> >
> > Volvo wrote:
> >>
> >>
> >> Hi,
> >>
> >> How can I generate a random number as
> >>
> >> -1 or +1 in a loop
> >>
> >> TIA
> >>
> >>
> >>
>

And a shortway would be:

A = sign(randn(n,1)); % Assumes P(x=0)=0 a.s.

-Aj

``` 0 11/3/2003 3:30:51 PM