I want to sum the elements of the matrix M according to the the values in the matrix R into the array d.
Theoretically, it's cannot be serialized, because the action of summing into one array (D) requires memory access to the same data.
I implemented it in the following way
for ind = 1: numel(R)
d(R(ind)) = d(R(ind)) + M(ind);
end
like @Andrew suggested in this related topic: How do I iterate through each element in an n-dimensional matrix in MATLAB?
The elements of the array R and not every large, but also not 1 or 2, it can be for example 1 to 15.
Is there a more efficient way to do it in Matlab, even if the "theoretical complexity" of the action would be worse ?
For it could be solved also by iterating over the possible values in R and summing the elements of M in indexes where R = val , or anything more "built-in" in Matlab, which don't "like" loops generally speaking.
In SQL for example you have a "built-in" method to collapse repetition of one column and get the sum of the values in the other column.
There is a topic about similar action but in different langauge : Collapse a matrix to sum values in one column by values in another