Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random -1's and 1's

Subject: Random -1's and 1's

From: Ulrik Nash

Date: 3 Sep, 2011 12:54:11

Message: 1 of 5

Hi Everyone,

What is the most efficient way of creating a vector filled with -1's and 1's, with probability(-1) = probability(1)?

Regards,

Ulrik

Subject: Random -1's and 1's

From: Nasser M. Abbasi

Date: 3 Sep, 2011 13:10:47

Message: 2 of 5

On 9/3/2011 5:54 AM, Ulrik Nash wrote:
> Hi Everyone,
>
> What is the most efficient way of creating a vector filled with -1's and 1's,
> with probability(-1) = probability(1)?
>
> Regards,
>
> Ulrik

I am sure there is a better way, but why not use randperm(2)
and just look at the first value returned, and if it is say
1, call this your '1', and if it is '2' call this your '-1'

Since randperm calls rand, and you have only 2 elements, then
the prob is 1/2 for each to come out?

---------------------
EDU>> N=20;
A=zeros(N,2);
for i=1:N
     A(i,:)=randperm(2);
end
A(A(:,1)==2)=-1;
A(:,1)
----------------

ans =

     -1
     -1
      1
     -1
      1
      1
     -1
      1
      1
      1
      1
      1
      1
     -1
     -1
      1
     -1
     -1
      1
     -1


--Nasser

Subject: Random -1's and 1's

From: Bruno Luong

Date: 3 Sep, 2011 13:34:11

Message: 3 of 5

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <j3t81j$rd$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> What is the most efficient way of creating a vector filled with -1's and 1's, with probability(-1) = probability(1)?
>

 v = 2*(rand(1,100) < 0.5) - 1

Bruno

Subject: Random -1's and 1's

From: Ulrik Nash

Date: 3 Sep, 2011 13:54:10

Message: 4 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j3tacj$75k$1@newscl01ah.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <j3t81j$rd$1@newscl01ah.mathworks.com>...
> > Hi Everyone,
> >
> > What is the most efficient way of creating a vector filled with -1's and 1's, with probability(-1) = probability(1)?
> >
>
> v = 2*(rand(1,100) < 0.5) - 1
>
> Bruno

Thank you both. I think I will go with Bruno's idea. Very neat!

Subject: Random -1's and 1's

From: Derek O'Connor

Date: 3 Sep, 2011 15:41:28

Message: 5 of 5

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <j3t81j$rd$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> What is the most efficient way of creating a vector filled with -1's and 1's, with probability(-1) = probability(1)?
>
> Regards,
>
> Ulrik

This generates a Bernoulli Process Xi = 1 with prob. p, Xi = -1 with prob. (1-p), i = 1,2,...,n.
function bern = BernProc(n,p)
     bern = -1+2*(rand(1,n) <= p);

Tags for this Thread

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.

Contact us