Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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 7

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 7

"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 7

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 7

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 7

"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 7

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].

Subject: Give me a good example for accumarray()

From: ole asdf

Date: 5 Sep, 2010 07:36:06

Message: 7 of 7

Ok, quite old thread, nevertheless I decided to post, because I'm quite happy right now, because I found out, accumarray() solves my problem perfectly:

Given you have a statistic "stat" in which results from different games are stored (one game per row). In the first column is a player id, in the second column a gain/loss for the player with a specific player id. The matrix "stat" contains infos about many games and one player can have multiple entries (one for every game he played). If you want to get the cumulated gain/loss for every player, you calculate: cumulated_score = accumarray(stat(:,1) , stat(:,2)) which returns a column vector. (Players with an id that is not contained in stat(:,1) will get a score of 0.) To get the cumulated score of player with id = 5 you enter cumulated_score(5).

Implementing this (in a fast manner) by yourself can be quite a PITA...

Tags for this Thread

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.

Contact us