Path: news.mathworks.com!not-for-mail
From: "Naor Movshovitz" <lazy_n@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: accessing the same elements repeatedly in a vectorized fashion?
Date: Wed, 1 Aug 2007 23:07:47 +0000 (UTC)
Organization: Tel-Aviv University
Lines: 24
Message-ID: <f8r3o3$7qj$1@fred.mathworks.com>
References: <f8r2uh$oib$1@fred.mathworks.com>
Reply-To: "Naor Movshovitz" <lazy_n@yahoo.com>
NNTP-Posting-Host: webapp-01-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1186009667 8019 172.30.248.36 (1 Aug 2007 23:07:47 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 1 Aug 2007 23:07:47 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 482202
Xref: news.mathworks.com comp.soft-sys.matlab:422114


The function accumarray is what you need.


"Ori " <john.doe.nospam@mathworks.com> wrote in message <f8r2uh$oib$1@fred.mathworks.com>...
> Hello, 
> 
> I would like to vectorize the following loop.
> 
> for i = 1:length(c)
>   a(b(i)) = a(b(i)) + c(i)
> end
> 
> However, a simple 
> 
>  a(b) = a(b) + c 
> 
> will not work as 'b' is not one-to-one. Matlab will only add one (the last, I think) value of 'c' to each corresponding 'a' element. I would like to add all the corresponding values in 'c', just like in the loop.
> 
> Any ideas?
> 
> Please note that in my particular case, the multiplicity of 'b' is not fixed or constant. That is, each element of 'a' is accessed a different number of times (between 3 to, say, 8 times) by 'b'. 
> 
> Thanks.