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

Thread Subject: can I avoid a loop here?

Subject: can I avoid a loop here?

From: Ross

Date: 19 Feb, 2008 17:51:41

Message: 1 of 3

Hi,
I face the following problem:

Let j and x be two vectors of same length.
I would like to apply function FUN to the subset of xs, where the
subset is defined by the index j.

--------------
Eg:

j = [1 2 2 2 3 3]'
x = [6 43 3 4 2 5]'

FUN is sum

the result wanted is [6 50 7]'
-----------------

Is there a way to avoid looping?

Thank you
Ross

Subject: Re: can I avoid a loop here?

From: Steven Lord

Date: 19 Feb, 2008 18:10:43

Message: 2 of 3


"Ross" <fed.rossi@gmail.com> wrote in message
news:e01a38fc-431a-415f-9162-8e7ad7a422c0@q33g2000hsh.googlegroups.com...
> Hi,
> I face the following problem:
>
> Let j and x be two vectors of same length.
> I would like to apply function FUN to the subset of xs, where the
> subset is defined by the index j.
>
> --------------
> Eg:
>
> j = [1 2 2 2 3 3]'
> x = [6 43 3 4 2 5]'
>
> FUN is sum
>
> the result wanted is [6 50 7]'
> -----------------
>
> Is there a way to avoid looping?

>> j = [1; 2; 2; 2; 3; 3];
>> x = [6; 43; 3; 4; 2; 5];
>> M = accumarray([j ones(size(j))], x)

M =

     6
    50
     7

--
Steve Lord
slord@mathworks.com


Subject: Re: can I avoid a loop here?

From: Ross

Date: 19 Feb, 2008 18:22:19

Message: 3 of 3

On Feb 19, 12:10 pm, "Steven Lord" <sl...@mathworks.com> wrote:
> "Ross" <fed.ro...@gmail.com> wrote in message
>
> news:e01a38fc-431a-415f-9162-8e7ad7a422c0@q33g2000hsh.googlegroups.com...
>
>
>
> > Hi,
> > I face the following problem:
>
> > Let j and x be two vectors of same length.
> > I would like to apply function FUN to the subset of xs, where the
> > subset is defined by the index j.
>
> > --------------
> > Eg:
>
> > j = [1 2 2 2 3 3]'
> > x = [6 43 3 4 2 5]'
>
> > FUN is sum
>
> > the result wanted is [6 50 7]'
> > -----------------
>
> > Is there a way to avoid looping?
> >> j = [1; 2; 2; 2; 3; 3];
> >> x = [6; 43; 3; 4; 2; 5];
> >> M = accumarray([j ones(size(j))], x)
>
> M =
>
> 6
> 50
> 7
>
> --
> Steve Lord
> sl...@mathworks.com

beautiful.
thanks Steve!

Tags for this Thread

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.

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