Using binornd with population and mortality rates

Hi, I'm using csvread to set the parameters (N and m) for the function binornd. I attached the parameters and the code is very straight forward
N=csvread('N.csv',1,0);
m=csvread('m.csv',1,0);
B= binornd(N,m);
The result B show many cells with NaN which is wrong. If I instead create the parameters N and m by manually loading the, i.e. for example N=[c1 c2 c3 ...] then there are no NaN which is the expected result. I used class to identify the parameters as double and can't figure out what is causing the problem or more importantly how to solve the problem. Any ideas?

댓글 수: 7

I cannot reproduce your results. When I run binornd with your matrices, I get no NaN values (in R2017a).
I'm using R2015b version. Maybe I'm using a to old version of Matlab? Strangely, I feed in the first cell into the function and received a value instead of NaN as shown in the matrix.
It could help if you posted the relevant parts of your code. I doubt that it is a bug in R2015b.
Hi, I have edited my query. Please read the question above.
I solved the problem 18 hours ago and posted it in my Answer.
Please Accept and vote for my Answer.
Unfortunately the solution doesn't work. The difference is your answer and mine is that I am reading in the parameters using csvread.
It worked when I ran it, or I would not have posted it. (I am using R2017a, although it should also work in all other recent versions.)
I used csvread as well, as I posted in my Answer, and actually used your code. The only change I made was to add:
N = fix(N);
to get rid of the fractional part of the numbers in the ‘N’ matrix. They have to be integers, and the fix call forces that. The code then works correctly.
Note that this line:
m = double(m);
is not necessary, and can be deleted. (It was part of my troubleshooting steps, and I forgot to delete it before I posted my Answer.) It has no effect, since ‘m’ is already a double array.

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

답변 (1개)

Star Strider
Star Strider 2017년 7월 1일
I found the problem!
Apparently, the ‘N’ array are not integers. They could be the result of calculations that result in very small floating-point approximation errors. The binornd function accepts only integers as the first argument, so will return NaN for any that are not.
Adding this line:
N = fix(N);
completely avoids the NaN results in the ‘D’ matrix.
N = csvread('N.csv',1,0);
m = csvread('m.csv',1,0);
N = fix(N);
m = double(m);
D = binornd(N,m);

댓글 수: 1

My code actually does work.
I attach a ‘.mat’ file with the ‘N’ and ‘D’ matrices it creates. There are no NaN or other non-finite values in the ‘D’ matrix.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 7월 1일

댓글:

2017년 7월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by