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:
How do you understand accumarray?

Subject: How do you understand accumarray?

From: Luna Moon

Date: 25 Jun, 2010 18:11:42

Message: 1 of 3

K>> subs = [1; 2; 4; 2; 4];
       A = accumarray(subs, 101:105)

A =

   101
   206
     0
   208

Why is that? I just don't get it...

Subject: How do you understand accumarray?

From: Sean

Date: 25 Jun, 2010 18:24:05

Message: 2 of 3

Luna Moon <lunamoonmoon@gmail.com> wrote in message <f85233fc-b174-48a9-be24-f4fdaf2d5a67@e5g2000yqn.googlegroups.com>...
> K>> subs = [1; 2; 4; 2; 4];
> A = accumarray(subs, 101:105)
>
> A =
>
> 101
> 206
> 0
> 208
>
> Why is that? I just don't get it...

Every time a sub encountered it's corresponding value is added. (The default function is plus)
Thus there is one 1 summing to 101. 2 sums 102,104 = 206;4 sums 103,105=208. There are no 3s so it doesn't get anything.

Subject: How do you understand accumarray?

From: Matt Fig

Date: 25 Jun, 2010 18:28:19

Message: 3 of 3

For this simple case,

% DATA
subs = [1; 2; 4; 2; 4];
val = [101;102;103;104;105];


% ENGINE
A = zeros(max(subs),1);

for ii = 1:length(subs)
   A(subs(ii)) = A(subs(ii)) + val(ii);
end

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