I'm just not understanding how exactly accumarray works. I checked the official matlab documentation, but I'm still unable to comepletely understand.
If it's something like this, where:
subs = [1
3
4
3
4]
val = 101:105';
accumarray(subs,val) =
[101
0
206
208]
I get that it was A(1)= 101, A(2)= 0, A(3)= 102+104, and A(4)= 103+105
But if it was something like:
subs = [1 1
2 2
3 2
1 1
2 2
4 1]
val = 101:106';
accumarray(subs,val) =
[205 0
0 207
0 203
106 0]
I don't understand how the method works.... I kinda get the A(1) = 101+104, and then that A(2) = 102+105.
But why is that displayed in the second column? Also, why is the [3 2] line not included in the calculation?
I know this is a really simple question, but this is the first time I'm using Matlab.. any help on this would be greatly appreciated, thank you!!
As described in the docs for accumarray:
Considering a call to the function like the following:
A = accumarray(subs,val)
The values in each row of the m-by-n matrix subs define an n-dimensional subscript into the output, A.
Therefore, in your case since subs
is a Something x 2
array, each of its row is considered a subscript pointing to the output A, which is of size 4x2
because the maximal values in each columns are respectively 4 and 2.
Therefore, if we decompose subs
into similar rows, i.e. similar subscripts, we see that there are 2 rows pointing to the same coordinates (1,1) as well as (2,2). There is only 1 pointing to (4,1) and 1 pointing to (3,2). Therefore, we would expect output A
to have accumulated values only at those coordinates:
(1,1)
(2,2)
(3,2)
(4,1)
which is exactly the case:
A =
205 0
0 207
0 103
106 0
Is that clearer?