필터 지우기
필터 지우기

Binornd draws '1' every time

조회 수: 1 (최근 30일)
Henry
Henry 2014년 7월 8일
댓글: Henry 2014년 7월 8일
I'm running a script in which I use binornd(1,0.5) within a for loop, and the output is '1' every time. When I copy paste to the command window, it behaves as expected (I haven't done a full statistical test, but at least I get both '0's and '1's as outputs). In the same script, I use randi, which seems to be behaving normally..
Moving the binornd line to different positions in the script does not make a difference, and neither does adding rng('shuffle') just above. Finally, I use binornd(1,p) in another loop, where p varies on each iteration, and I also get only '1's as outputs. So it seems to be a problem specific to binornd.
  댓글 수: 5
Henry
Henry 2014년 7월 8일
Apologies, this was not supposed to be included - I used it as a test, but it didn't resolve the problem (although it did slow things down as you say).
I've now found another clue - even randi([0 1]) is causing the same problem, although randi with non-binary arguments is working fine, and again randi([0 1]) works fine from the command line! Very strange...
Henry
Henry 2014년 7월 8일
Can't see why, but problem resolved after making some changes elsewhere in the code. Thanks everyone.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jos (10584)
Jos (10584) 2014년 7월 8일
what if you execute
which binornd
just before the calls? Do the outputs differ based on the situation (script vs. command line). It could be that you have two different versions ...
  댓글 수: 1
Henry
Henry 2014년 7월 8일
Thanks for the response. I tried this and I get the same version both times: C:\Program Files\MATLAB\R2014a\toolbox\stats\stats\binornd.m
But yes the outputs do differ between these 2 situations. I've now noticed that the 2nd time I call it, it is indeed working (just that the p values were high), but the first time is still failing (line 5 of the code I entered above). And changing p to any non-zero value below 1 does not change the situation.

댓글을 달려면 로그인하십시오.


Shashank Prasanna
Shashank Prasanna 2014년 7월 8일
편집: Shashank Prasanna 2014년 7월 8일
Why don't you generate all of your random numbers before hand and just use index into the array at each loop iteration? It should be faster and cleaner:
randBin = binornd(limit,0.5);
while count<limit
matrix=fastest_matrix;
a(count)=randi([1 order]);
b(count)=randi([1 order]);
t(count)=randBin(count);
...
  댓글 수: 3
Shashank Prasanna
Shashank Prasanna 2014년 7월 8일
My bad, I meant to say:
randBin = binornd(1,0.5,limit,1);
Henry
Henry 2014년 7월 8일
OK thanks. And the problem resolved itself a few minutes ago when I made some changes somewhere else in the code. It's still not at all clear to me why, but at least it's working!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by