Borrar filtros
Borrar filtros

Using binornd with population and mortality rates

1 visualización (últimos 30 días)
Orongo
Orongo el 1 de Jul. de 2017
Comentada: Star Strider el 2 de Jul. de 2017
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 comentarios
Orongo
Orongo el 2 de Jul. de 2017
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 el 2 de Jul. de 2017
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.

Iniciar sesión para comentar.

Respuestas (1)

Star Strider
Star Strider el 1 de Jul. de 2017
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 comentario
Star Strider
Star Strider el 2 de Jul. de 2017
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by