# What is the equivalent of MATLAB's accumarray?

Is there any equivalent of MATLAB's accumarray?

I'm looking for an equivalent of:

accumarray(A(:),B(:),[],@mean)


where A and B are two matrix datasets.

EDIT: As asked in the comment, I will provide a bit more from the MATLAB code. As far as I understood, the first matrix contains a set of radius values (all integer values), the second contains a random pattern (here 0's and 1's) like this:

(* A(:) *)
a = Table[Round@Sqrt[x^2 + y^2], {y, -5, 5, 1.}, {x, -5, 5, 1}];
(* B(:) *)
SeedRandom;
b = Table[x y, {y, RandomInteger[1, 5]}, {x, RandomInteger[1, 5]}]


Now accumarray(A(:),B(:),[],@mean) seems to virtually collect all matrix elements of $b$ where the elements $a_{ij}$ share the same value and than the function @mean is applied to all those virtually collected values. After this the obtained values are written in a List containing {Value of a, mean of all values of b}. I think this is what the code is supposed to do.

• Any $n \times m$ Matrix, Input as well as output.
– Kay
Aug 17, 2017 at 12:44
• @Kay This site is about Mathematica, information not related to it should be included or linked, that is quite obvious. Unless we talk about common knowledge, but the definition of accumarray does not seem to be part of it.
– Kuba
Aug 17, 2017 at 13:00
• @Kuba Thanks for your hint! I'll try to respect that for the next time!
– Kay
Aug 17, 2017 at 13:04
• I'm not sure I understand. All you need to do is explain what you want to do in plain English, and not rely on a piece of MATLAB. The question should be understandable even if that MATLAB code is removed. Or are you saying that you do not fully understand what accumarray(A(:),B(:),[],@mean) does? Aug 17, 2017 at 13:20
• @Kay actually, I doubt that MORE matlab code would help. I was hoping you could show us a simple example of the inputs you would give to this function in Mathematica notation, and the output you desire, also in Mathematica notation, together with a description in English of the operation you want to carry out. If you don't understand what that piece of code does, you might go ask about that on a MATLAB forum first. Aug 17, 2017 at 13:26

 SeedRandom;
n = 10;
m = 100;
f = Mean;
a = RandomInteger[{1, n}, {m, 2}];
b = RandomInteger[{0, 1}, {m}];


The first application of accumarray that came to my mind is the assembly of SparseArrays. This can be directly done with

sparseresult = With[{spopt = SystemOptions["SparseArrayOptions"]},
InternalWithLocalSettings[
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> f@*List}],
SparseArray[a -> b],
SetSystemOptions[spopt]]
];
sparseresult["NonzeroValues"]


A more flexible (but not as efficient way) to do it is by using GrouBy as follows

groupbyresult = Map[
f,
GroupBy[Transpose[{a, b}], First -> Last]
]


An alternative way can be obtained with Merge

mergeresult = Merge[

Note that the ordering of the resulting array is not uniquely defined. Hence, using Associations is a robust way to represent it.
• What is the purpose of Flatten@b in groupbyresult? b already has a flattened form?!
• For safety, one might consider using InternalWithLocalSettings[] (see e.g. this) when playing around with SetSystemOptions[]. Aug 17, 2017 at 15:39
• Probably the only thing I would have done differently would be to put in sparseresult = SparseArray[a -> b]; sparseresult["NonzeroValues"] (or even SparseArray[a -> b] @ "NonzeroValues" if you like things terse) as the second argument to InternalWithLocalSettings[]`; as mentioned in the answer I linked to, whatever is in the second argument is automatically returned. But, your way works too. Aug 18, 2017 at 9:07