Thread Subject: Give me a good example for accumarray()

Subject: Give me a good example for accumarray()

From: Husam Aldahiyat

Date: 4 Jul, 2009 08:54:01

Message: 1 of 6

Hello,
I am learning MATLAB and when I read about the accumarray() function it looks good but I didn't understand how the examples worked.

Could someone give me a nice small example (problem) and how accumarray solves it?

Subject: Give me a good example for accumarray()

From: John D'Errico

Date: 4 Jul, 2009 09:55:02

Message: 2 of 6

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <h2n5b9$lo7$1@fred.mathworks.com>...
> Hello,
> I am learning MATLAB and when I read about the accumarray() function it looks good but I didn't understand how the examples worked.
>
> Could someone give me a nice small example (problem) and how accumarray solves it?

Any time that you wish to count the number of times
an element appears in a vector, accumarray is the
solution. Best is if the elements are small positive
integers.

x = ceil(10*rand(1,20))
x =
 7 1 9 10 7 8 8 4 7 2 8 1 3 1 1 9 7 4 10 1

count = accumarray(x',1)
count =
     5
     1
     1
     2
     0
     0
     4
     3
     2
     2

If they are not small positive integers, then the third
argument of unique will provide the extra piece.

If you wish to see accumarray in action, it forms an
important part of my own consolidator utility on the
file exchange:

http://www.mathworks.com/matlabcentral/fileexchange/8354

Of course, accumarray has other uses, since it can do
more than just count a list of numbers. But, this is
the task I set to accumarray more often than any other.

John

Subject: Give me a good example for accumarray()

From: Matt Fig

Date: 4 Jul, 2009 15:26:01

Message: 3 of 6

Here's an odd one.

Say you wanted to find the row number in which each unique value in a matrix of positive integers appears, then store these row numbers in a cell array. (Don't ask.)

M = ceil(rand(20,3)*10); % Data.
C = accumarray(M(:), repmat((1:size(M,1))', [size(M,2) 1]), [], @(x) {x});

Subject: Give me a good example for accumarray()

From: Husam Aldahiyat

Date: 4 Jul, 2009 18:32:00

Message: 4 of 6

Thanks for the responses. I'm trying to learn of it and of arrayfun (I'm good with cellfun but having troubles with arrayfun). I want to do this for example:

say I have a matrix s;

s =

     1 1
     6 1
     1 2
     5 2
     1 3
     2 3
     8 4
     1 7
     3 7
     7 7
     3 8
     8 8

I want to find the rows which have a similar first column and a second column that is different by +/- 1.

For example for first column value (1) I will get

1 1
1 2
1 3

and for first column value (3)

3 7
3 8

I can do a loop but my one liner attempts have failed. I'm trying this but it's wrong. Can someone please tell me what I'm doing wrong and if there is a way using accumarray or arrayfun? Thanks!!!

% for first column value 1 I want second column values that are close
s(arrayfun(@(x,y)x==1 && sum(ismember(y(x==1),y-1:y+1)),s(:,1),s(:,2)),2)

Help!

Subject: Give me a good example for accumarray()

From: Matt Fig

Date: 4 Jul, 2009 18:57:01

Message: 5 of 6

"Husam Aldahiyat" <numandina@gmail.com> wrote in message
> say I have a matrix s;
>
> s =
>
> 1 1
> 6 1
> 1 2
> 5 2
> 1 3
> 2 3
> 8 4
> 1 7
> 3 7
> 7 7
> 3 8
> 8 8
>
> I want to find the rows which have a similar first column and a second column that is different by +/- 1.
>
> For example for first column value (1) I will get
>
> 1 1
> 1 2
> 1 3
>
> and for first column value (3)
>
> 3 7
> 3 8
>

How is (7,8) within +/-1 of 3? If you mean to find the values in the second column which are +/-1 from each other, what do you expect when there are two such sets? Are we to return both sets? For example:

s =
3 7
3 8
2 9
1 0
3 60
3 61


Maybe posting your for loop would clarify this matter.

Subject: Give me a good example for accumarray()

From: Peter Perkins

Date: 6 Jul, 2009 15:55:40

Message: 6 of 6

Husam Aldahiyat wrote:

> For example for first column value (1) I will get
>
> 1 1
> 1 2
> 1 3
>
> and for first column value (3)
>
> 3 7
> 3 8

ACCUMARRAY may or may not be able to help here. The SUBS input determines how values in the VALS input are batched up when passed to your FUN, but FUN has no access to the SUBS values themselves. Thus, AUUMARRAY can easily return [1; 2; 3], but you will need to do something additional to get [1 1; 1 2; 1 3].

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
arrayfun Husam Aldahiyat 4 Jul, 2009 14:34:07
accumarray Husam Aldahiyat 4 Jul, 2009 04:59:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com