Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: accessing the same elements repeatedly in a vectorized fashion?

Subject: accessing the same elements repeatedly in a vectorized fashion?

From: Ori

Date: 01 Aug, 2007 22:54:09

Message: 1 of 4

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.

Subject: Re: accessing the same elements repeatedly in a vectorized fashion?

From: Naor Movshovitz

Date: 01 Aug, 2007 23:07:47

Message: 2 of 4

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.

Subject: Re: accessing the same elements repeatedly in a vectorized fashion?

From: Ori

Date: 02 Aug, 2007 02:29:29

Message: 3 of 4

Thanks!

"Naor Movshovitz" <lazy_n@yahoo.com> wrote in message <f8r3o3$7qj$1@fred.mathworks.com>...
> 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.
>

Subject: Re: accessing the same elements repeatedly in a vectorized fashion?

From: us

Date: 02 Aug, 2007 06:03:04

Message: 4 of 4

Ori:
<SNIP wants to accumulate his/her values...

as others said

     help accumarray;

-and- look at <loren shure>'s contribution here

http://blogs.mathworks.com/loren/2007/07/26/assignment-with-repeated-indices/

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
accumarray us 02 Aug, 2007 02:05:43
reference us 02 Aug, 2007 02:05:43
vectorize Ori 01 Aug, 2007 18:55:18
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics