Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!backlog2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 02 Jul 2009 13:27:14 -0500
Date: Thu, 02 Jul 2009 14:27:13 -0400
From: MZ <mark@nospam.void>
User-Agent: Thunderbird 2.0.0.22 (Windows/20090605)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: randsample bug?
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <zJSdnVIvjZGfYdHXnZ2dnUVZ_tidnZ2d@giganews.com>
Lines: 32
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-8Nm85X1FrArbA71AGh5mshhtY7FiZY7xb8XBJQiaxZSiJqIknOaR6FBB9c9ZjI6xxnN69v3hUr3rl9N!fa3JvuEeGfaTxF73uLOOIuBQlc4in5g8+4IjpikrrV/l6dB5Yn9D/wNRsl4amBauu4VoL6n52s4g!wQ==
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.39
X-Original-Bytes: 2066
Xref: news.mathworks.com comp.soft-sys.matlab:552485


I may have found a bug in randsample.  Can anyone suggest a workaround?

I'm calling it with:

randsample(vals,10^5,true,w);

vals is the data set I'm sampling from.  w is a weighting function. 
Some of the elements of w are equal to zero.

Occasionally I get an error:

"??? Subscript indices must either be real positive integers or 
logicals."

And it points to the line of code in randsample.m that reads:

y = population(y);

I checked, and apparently y is equal to 0 for one or two of the 10^5 
values in y.  Where does y come from?

[dum, y] = histc(rand(k,1),[0 cumsum(p)]);

where p is the normalized weighting array.

That means that histc is returning 0 in y every so often, indicating 
that the rand function is out of range.  But rand is supposed to return 
(0,1), right?  So that means that the cumsum(p) part is not going from 0 
to 1?  In fact, when I ask it if max(cumsum(p))>1, it returns true. 
Seems the opposite of what I was expecting.

So, I'm completely confused now.