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
Star Strider
2017년 7월 1일
I cannot reproduce your results. When I run binornd with your matrices, I get no NaN values (in R2017a).
Orongo
2017년 7월 1일
Star Strider
2017년 7월 1일
It could help if you posted the relevant parts of your code. I doubt that it is a bug in R2015b.
Orongo
2017년 7월 2일
Star Strider
2017년 7월 2일
I solved the problem 18 hours ago and posted it in my Answer.
Please Accept and vote for my Answer.
Orongo
2017년 7월 2일
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
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
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.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!