a matlab code of getting a value uniformly at random from a the set {-1,1}

1 次查看(过去 30 天)
I need to get a value uniformly at random from a the set {-1,1}

回答(5 个)

Massimo Zanetti
Massimo Zanetti 2016-10-11
编辑:Massimo Zanetti 2016-10-12
If you look for a real number in the interval [-1,1], here is it
n = 2*rand-1
If you look for an integer either 1 or -1, here is it
n = 2*randi(2)-3
  2 个评论
Massimo Zanetti
Massimo Zanetti 2016-10-12
编辑:Massimo Zanetti 2016-10-12
The first one returns a random REAL number in the interval [-1,1]. That is why it doesn't return only -1 or 1.
The second one, I have fixed it. Now it returns a random integer among {-1,1}.

请先登录,再进行评论。


Matthew Eicholtz
Matthew Eicholtz 2016-10-11
To generate values uniformly from the set {-1,1}, use:
N = 100;
x = 2*(rand(N,1)>0.5)-1;
where N is the number of values you want.

Walter Roberson
Walter Roberson 2016-10-12
>> N=10000000;
>> timeit(@() 2*(rand(N,1)>0.5)-1, 0)
ans =
0.1797
>> timeit(@() 2*randi(2,N,1)-3, 0)
ans =
0.1467
>> timeit(@() 2*round(rand(N,1))-1, 0)
ans =
0.1269
  1 个评论
Walter Roberson
Walter Roberson 2016-10-14
Using
N=10000000;
clear x;tic;x=2*round(rand(N,1))-1;toc %"round"
clear x;tic;x=2*(rand(N,1)>0.5)-1;toc %"rand>"
clear x;tic;x=2*randi(2,N,1)-3;toc %"randi"
and taking the lowest of several timings for each of the three, I record
same Virtual machine with Windows 10 (host is Mac)
  • R2011b: round 0.169, rand> 0.187, randi 0.218
  • R2012b: round 0.165, rand> 0.191, randi 0.220
  • R2013b: round 0.172, rand> 0.193, randi 0.220
  • R2015a: round 0.179, rand> 0.180, randi 0.169
  • R2015b: round 0.176, rand> 0.184, randi 0.155
  • R2016a: round 0.166, rand> 0.178, randi 0.156
  • R2016b: round 0.165, rand> 0.176, randi 0.150
So round did not change much; rand> got slightly faster; and randi improved a fair bit
Native on host (OS-X El Capitan)
  • R2014b: round 0.141, rand> 0.214, randi 0.144 %but randi was faster on average
  • R2015a: round 0.140, rand> 0.238, randi 0.148 %but randi was faster on average
  • R2015b: round 0.127, rand> 0.169, randi 0.143
  • R2016a: round 0.105, rand> 0.144, randi 0.122
  • R2016b: round 0.093, rand> 0.165, randi 0.117
so the three all got faster, but not all at the same time
The times to process rand> were sometimes worse on native OS-X than on emulated Windows 10, but the final times are pretty comparable; it might even be the case that if I were to boot into Windows 10 that the times for rand> might be better than running it in OS-X .
The final processing times for round are much better on OS-X
randi appears to have gotten faster on OS-X as of R2016a.
I note that the timing of randi getting faster differs between operating systems.

请先登录,再进行评论。


kortas manel
kortas manel 2016-10-11
thank you for your answerers

James Tursa
James Tursa 2016-10-11
编辑:James Tursa 2016-10-11
Yet another way (although Matthew's Answer is faster so I voted for it):
N = number of values
R = 2*randi(2,N,1) - 3;
  6 个评论
James Tursa
James Tursa 2016-10-12
I happened to be using PCWIN 32-bit R2011b for the testing, which doesn't have timeit. However, here are the manual timing results:
>> version
ans =
7.13.0.564 (R2011b)
>>
>> N=10000000;
>>
>> clear x;tic;x=2*(rand(N,1)>0.5)-1;toc
Elapsed time is 0.206160 seconds.
>> clear x;tic;x=2*(rand(N,1)>0.5)-1;toc
Elapsed time is 0.181466 seconds.
>> clear x;tic;x=2*(rand(N,1)>0.5)-1;toc
Elapsed time is 0.182702 seconds.
>>
>>
>> clear x;tic;x=2*randi(2,N,1)-3;toc
Elapsed time is 0.259775 seconds.
>> clear x;tic;x=2*randi(2,N,1)-3;toc
Elapsed time is 0.244608 seconds.
>> clear x;tic;x=2*randi(2,N,1)-3;toc
Elapsed time is 0.251186 seconds.
>>
>>
>> clear x;tic;x=2*round(rand(N,1))-1;toc
Elapsed time is 0.151603 seconds.
>> clear x;tic;x=2*round(rand(N,1))-1;toc
Elapsed time is 0.164555 seconds.
>> clear x;tic;x=2*round(rand(N,1))-1;toc
Elapsed time is 0.150253 seconds.
So, the rand beats randi for my particular setup, but your round method is better yet. I'll have to remember that one! (and give you a vote)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by