Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using mnrnd: NaN output

Subject: using mnrnd: NaN output

From: Erida Gjini

Date: 20 Jun, 2008 10:03:02

Message: 1 of 5

Hi,
I am trying to use the multinomial random variable function
in Matlab.

PROB=v'/sum(v); % row vector
num_events=mnrnd(1,PROB);

Sometimes the simulation gives a correct sequence of zeros
and a single 1, as I expect. Sometimes though, I get all
NaN values in the num_events. I checked my PROB variable and
it works fine. It satisfies the condition that it is always
between zero and 1. PROB is calculated anew each time, so it
decreases in dimension, but I don't store it anywhere. It is
overwritten. Any ideas?
Cheers,
Erida

Subject: using mnrnd: NaN output

From: Peter Perkins

Date: 20 Jun, 2008 13:39:27

Message: 2 of 5

Erida Gjini wrote:
> Hi,
> I am trying to use the multinomial random variable function
> in Matlab.
>
> PROB=v'/sum(v); % row vector
> num_events=mnrnd(1,PROB);
>
> Sometimes the simulation gives a correct sequence of zeros
> and a single 1, as I expect. Sometimes though, I get all
> NaN values in the num_events. I checked my PROB variable and
> it works fine. It satisfies the condition that it is always
> between zero and 1. PROB is calculated anew each time, so it
> decreases in dimension, but I don't store it anywhere. It is
> overwritten. Any ideas?

Just because v sums to 1 does not mean that it is a valid probability vector.
You can diagnose this by using an example of an input vector for which MNRND
returns NaNs, and stepping through the code in the debugger. If you cannot find
such an example, make a local copy of MNRND, with this at the end:

    if (any(isnan(r))
        p
    end

and set a breakpoint at the line that displays p, and run your simulation. When
the debugger stops at that line, look at p.

Hope this helps.

Subject: using mnrnd: NaN output

From: Erida Gjini

Date: 23 Jun, 2008 11:27:01

Message: 3 of 5

Thanks for the suggestion. I am still struggling to understand why my vector
is not a valid probability vector though. I don't spot any strange features in
the PROB vector that yields NaN values. Is the fact that some probabilities are
equal a problem? Or that there are some probabilities that are zero?
I hope someone can help me through this. I feel I am missing something
simple.
Thanks,
Erida




Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote in message
<g3gbuf$1op$1@fred.mathworks.com>...
> Erida Gjini wrote:
> > Hi,
> > I am trying to use the multinomial random variable function
> > in Matlab.
> >
> > PROB=v'/sum(v); % row vector
> > num_events=mnrnd(1,PROB);
> >
> > Sometimes the simulation gives a correct sequence of zeros
> > and a single 1, as I expect. Sometimes though, I get all
> > NaN values in the num_events. I checked my PROB variable and
> > it works fine. It satisfies the condition that it is always
> > between zero and 1. PROB is calculated anew each time, so it
> > decreases in dimension, but I don't store it anywhere. It is
> > overwritten. Any ideas?
>
> Just because v sums to 1 does not mean that it is a valid probability vector.
> You can diagnose this by using an example of an input vector for which
MNRND
> returns NaNs, and stepping through the code in the debugger. If you
cannot find
> such an example, make a local copy of MNRND, with this at the end:
>
> if (any(isnan(r))
> p
> end
>
> and set a breakpoint at the line that displays p, and run your simulation.
When
> the debugger stops at that line, look at p.
>
> Hope this helps.

Subject: using mnrnd: NaN output

From: Peter Perkins

Date: 23 Jun, 2008 13:04:22

Message: 4 of 5

Erida Gjini wrote:
> Thanks for the suggestion. I am still struggling to understand why my vector
> is not a valid probability vector though. I don't spot any strange features in
> the PROB vector that yields NaN values. Is the fact that some probabilities are
> equal a problem? Or that there are some probabilities that are zero?
> I hope someone can help me through this. I feel I am missing something
> simple.

It's pretty hard to say without seeing an example.

Subject: using mnrnd: NaN output

From: John D'Errico

Date: 23 Jun, 2008 13:17:01

Message: 5 of 5

Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote in message
<g3o70m$n59$1@fred.mathworks.com>...
> Erida Gjini wrote:
> > Thanks for the suggestion. I am still struggling to understand why my
vector
> > is not a valid probability vector though. I don't spot any strange features
in
> > the PROB vector that yields NaN values. Is the fact that some probabilities
are
> > equal a problem? Or that there are some probabilities that are zero?
> > I hope someone can help me through this. I feel I am missing something
> > simple.
>
> It's pretty hard to say without seeing an example.

Zero does not matter. Even an imaginary
element does not produce nans. So I'll
conjecture the problem is a negative
number.

P = rand(1,4);P(1) = 0;P = P/sum(P)
P =
            0 0.07558 0.46543 0.45899

mnrnd(1,P)
ans =
     0 0 1 0


P = rand(1,4);P(1) = -.1;P = P/sum(P)
P =
    -0.072496 0.10286 0.30576 0.66387

mnrnd(1,P)
ans =
   NaN NaN NaN NaN

John

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us