Using binornd with population and mortality rates

2 ビュー (過去 30 日間)
Orongo
Orongo 2017 年 7 月 1 日
コメント済み: Star Strider 2017 年 7 月 2 日
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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by