Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!newspeer.monmouth.com!newspeer1.nac.net!border2.nntp.dca.giganews.com!nntp.giganews.com!pd7cy1no!pd7cy2no!shaw.ca!pd7urf3no.POSTED!53ab2750!not-for-mail
X-Trace-PostClient-IP: 24.79.146.116
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.16 (Windows/20080708)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: help vectorizing a FOR loop
References: <g9oskl$st1$1@fred.mathworks.com>
In-Reply-To: <g9oskl$st1$1@fred.mathworks.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 37
Message-ID: <cPSvk.66035$hx.50905@pd7urf3no>
Date: Thu, 04 Sep 2008 15:05:44 GMT
NNTP-Posting-Host: 24.66.94.143
X-Complaints-To: abuse@shaw.ca
X-Trace: pd7urf3no 1220540744 24.66.94.143 (Thu, 04 Sep 2008 09:05:44 MDT)
NNTP-Posting-Date: Thu, 04 Sep 2008 09:05:44 MDT
Bytes: 2085
Xref: news.mathworks.com comp.soft-sys.matlab:488602



GG wrote:
 
> I'm hoping someone can help me vectorize the following 
> code. The tricky part is that bin_width is a function of 
> the loop-index i.

> for i = 1:1:10000
>   smoothline(i) = mean(data(i-bin_width(i)/2:i+bin_width
> (i)/2)
> end

> This code is just a sliding-window average to smooth-out a 
> noisy data array. The smoothed line is created at each 
> data point by looking at bin_width(i)/2 datapoints ahead 
> and bin_width(i)/2 datapoints behind, and taking the mean 
> of all this data. Then it steps to the next datapoint and 
> repeats with a new bin_width(i) value.

cumsum() the data. Suppose we call that csdata.
Then the mean "around" point i is
(csdata(i+bin_width(i)/2) - csdata(i-bin_width(i)/2)) / (bin_width(i)+1)

Then provided that bin_width is vectorized, the transformed
expression should be vectorizable:

csdata = cumsum(data);
maxi = 10000;
bw = bin_width(1:maxi);
idxhigh = (1:maxi) + bw/2;
idxlow = (1:maxi) - bw/2;
smoothline = (csdata(idxhigh) - csdata(idxlow)) ./ (bw+1);


-- 
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end