help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Choose index into array of probabilities


From: Søren Hauberg
Subject: Re: Choose index into array of probabilities
Date: Fri, 25 Aug 2006 12:34:37 +0200

fre, 25 08 2006 kl. 09:47 +0200, skrev Olaf Till:
> On Thu, Aug 24, 2006 at 08:29:52PM +0200, Søren Hauberg wrote:
> > tor, 24 08 2006 kl. 20:18 +0200, skrev Søren Hauberg:
> > > tor, 24 08 2006 kl. 23:38 +0530, skrev Masatran (Deepak), R.:
> > > > I have an array, of probabilities which add up to 1. How can I generate 
> > > > an
> > > > index into this array with each array cell being chosen with probability
> > > > equal to the cell value?
> > > Assuming p is the array of probabilities:
> > > 
> > >   cs = cumsum(a);
> > >   idx = find(cs >= rand());
> > Okay, this getting embarresing:
> >     cs = cumsum(p);
> 
> Sorry for meddling in, but should this not be:
>       cs = cumsum (p(:));
> 
> Otherwise `cumsum' sums each column separately.
Depends on wether p is a row- or a column vector, but your way handles
both cases.

> 
> Olaf
> 
> >     idx = find(cs >= rand())(1);
> > 
> > Søren
> > 
> > P.S. I shouldn't be answering questions, when I'm tired.
> > 
> > > 
> > > This should do it. I think I've written those line of code 100 times
> > > (it's a central part of the particle filter)
> > > 
> > > Søren
> > > 
> > > > _______________________________________________
> > > > Help-octave mailing list
> > > > address@hidden
> > > > https://www.cae.wisc.edu/mailman/listinfo/help-octave
> > > 
> > > 
> > > _______________________________________________
> > > Help-octave mailing list
> > > address@hidden
> > > https://www.cae.wisc.edu/mailman/listinfo/help-octave
> > 
> > 
> > _______________________________________________
> > Help-octave mailing list
> > address@hidden
> > https://www.cae.wisc.edu/mailman/listinfo/help-octave
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
> 
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]