Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorizing challenge
Date: Wed, 20 May 2009 09:17:16 +0000 (UTC)
Organization: Erasmus MC
Lines: 54
Message-ID: <gv0hqs$p9n$1@fred.mathworks.com>
References: <guvhl1$qif$1@fred.mathworks.com> <see-FBBAFE.21132019052009@news.frontiernet.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1242811036 25911 172.30.248.35 (20 May 2009 09:17:16 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 20 May 2009 09:17:16 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:541191


Doug Schwarz <see@sig.for.address.edu> wrote in message <see-FBBAFE.21132019052009@news.frontiernet.net>...
> In article <guvhl1$qif$1@fred.mathworks.com>,
>  "Heywood " <heywoodj123@yahoo.com> wrote:
> 
> > Hi all,
> > 
> > I'm having a hard time trying to vectorize the following code. Suppose I have 
> > a vector of interest V, and a reference vector R which is monotonically 
> > increasing (it's actually a cumulative distribution function). Both vectors 
> > contain only real numbers, except that I've modified R by tacking on -Inf at 
> > the beginning and +Inf at the end (the reason for which is hopefully obvious 
> > from the code below).
> > 
> > For each element of V, I want to find the first element of R that is greater 
> > than or equal to it.
> > 
> > To do this with a FOR loop is trivial (and yes, I realize the two lines in 
> > the loop can be combined into one; writing it this way just for clarity):
> > 
> > result = zeros(1,length(V));
> > for kk = 1:length(V);
> >   ind = find( V(kk) <= R, 1, 'first' );
> >   result(kk) = R(ind);
> > end;
> > 
> > But when V is very large, this is slow. Can anyone think of a clean way to 
> > 'vectorize' the FIND command?
> > 
> > Thanks very much in advance!
> > 
> > HJ
> 
> How about this?
> 
>   % Create some sample data.
>   dist = rand(1,10);
>   R = [0,cumsum(dist)];
>   R = R/R(end);
> 
>   % Find result.
>   n = length(R);
>   V = rand(1,100);
>   k = interp1(R,0:n,V);
>   result = R(ceil(k) + 1);
> 
> You are still finding the appropriate interval, but it's done 
> efficiently inside interp1.
 
or

[dummy, k2] = histc(V,R) 
result2 = R(k2+1) ;

Jos