MATLAB Language Finding the maximum value among elements grouped by another vector


This is an official MATLAB example

Consider the following code:

month = [1;1;2;3;8;1;3;4;9;11;9;12;3;4;11];
temperature = [57;61;60;62;45;59;64;66;40;56;38;65;61;64;55];
maxTemp = accumarray(month,temperature,[],@max);

The image below demonstrates the computation process done by accumarray in this case:

Explanation of computation process

In this example, all values that have the same month are first collected, and then the function specified by the 4th input to accumarray (in this case, @max) is applied to each such set.