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

조회 수: 1 (최근 30일)
kortas manel
kortas manel 2016년 10월 11일
댓글: Walter Roberson 2016년 10월 14일
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
James Tursa
James Tursa 2016년 10월 11일
Neither of these produce results from the set {-1,1} as requested.
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