Using binornd with population and mortality rates

1 次查看(过去 30 天)
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 个评论
Orongo
Orongo 2017-7-2
Unfortunately the solution doesn't work. The difference is your answer and mine is that I am reading in the parameters using csvread.
Star Strider
Star Strider 2017-7-2
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 个评论
Star Strider
Star Strider 2017-7-2
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.

请先登录,再进行评论。

类别

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