|
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
|