on the use of the accumarray function
Show older comments
Could someone please explain to me how the accumarray function works. For example:
val = 101:105;
subs = [1; 2; 4; 2; 4];
A = accumarray(subs,val)
A =
101
206
0
208
I cant understand why it performs the following calculations:
101 % A(1) = val(1) = 101
206 % A(2) = val(2)+val(4) = 102+104 = 206
0 % A(3) = 0
208 % A(4) = val(3)+val(5) = 103+105 = 208
Accepted Answer
More Answers (1)
Jakob Hannibal
on 23 Nov 2014
When doing this:
vals=101:106';
subs= [1 1;2 2;3 2;1 1;2 2;4 1;]
A=accumarray(subs,vals)
A =
205 0
0 207
0 103
106 0
I don't get this? Shouldn't it be:
A=
205 311
207 310
104 0
106 0
1 Comment
Sean de Wolski
on 24 Nov 2014
No, each row of subs is an index combination. [3 1] is not one so A(3,1) should equal fill value. Consider it with just one rather than vals
subs= [1 1;2 2;3 2;1 1;2 2;4 1;]
A=accumarray(subs,1)
A =
2 0
0 2
0 1
1 0
Which is exactly what we see for subs: 2 sets of [1, 1], two sets of [2,2], one [3,2], and one [4,1]
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!