As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output"
Am I wrong somewhere?
I ran the following code.
A = [2 10 13 ; 1 11 14; 1 12 10]
[U,ix,iu]= unique(A(:,1))
vals = reshape(A(:, 2:end).', [], 1)
subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1)
r2 = accumarray(subs, vals', [], @(x){x'})
r2{1}
r2{2}
A =
2 10 13
1 11 14
1 12 10
U =
1
2
ix =
3
1
iu =
2
1
1
vals =
10
13
11
14
12
10
subs =
2
2
1
1
1
1
r2 =
[1x4 double]
[1x2 double]
ans =
12 11 14 10
ans =
13 10
=========================
But I expected r{1} = 11 14 12 10
, and r{2} = 10 13
.
Why did accumarray suddenly changed the ordering?
How can I get the expected result?
The documentation of accumarray says:
And your
subs
is not sorted (at least not in ascending order). If you rewrite the code so thatsubs
is sorted andvals
is also rearranged accordingly you get the desired result:And running this code returns: