a matlab code of getting a value uniformly at random from a the set {-1,1}
2 ビュー (過去 30 日間)
古いコメントを表示
I need to get a value uniformly at random from a the set {-1,1}
0 件のコメント
回答 (5 件)
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
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
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.
0 件のコメント
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
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.
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
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)
Walter Roberson
2016 年 10 月 12 日
timeit is also in the File Exchange; https://www.mathworks.com/matlabcentral/fileexchange/18798-timeit-benchmarking-function
参考
カテゴリ
Help Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!