Matlab按组运行最大值

时间:2015-12-10 00:47:03

标签: matlab max groupwise-maximum

在Matlab中,如何为每个组计算数组的运行最大值(由另一个数组subs标记)?例如,将数组subs视为3名学生的标签,并将val中的相应值视为考试成绩,我想计算每个学生达到的最高分数。

>> subs = [1; 3; 1; 1; 3; 2];
>> val = [101 102 103 98 105 106];

所需的输出与val的大小相同,并给出该学生当前的最高分数:

output = [101, 102, 103, 103, 105, 106]

我的数据集非常大,有数百万个条目,所以我想避免使用for循环。如果我只想要每个学生的最高分,我会使用accumarray(subs,val,[],@max)但是这里的问题更加困难,因为我想要运行最大值。

R中有类似的问题,但我希望能够在Matlab中做到这一点。 Finding running maximum by group in R

谢谢!

1 个答案:

答案 0 :(得分:4)

If you have a recent Matlab version you can use accumarray with cummax as follows. Note that subs needs to be sorted first (and of course the same sorting needs to be applied to vals and undone at the end).

[subsSorted, ind] = sort(subs); %// make sure grouping variable is sorted, so that
    %// accumarray is stable
result = accumarray(subsSorted, val(ind), [], @(x) {cummax(x).'}); %'// running max of
    %// each group is computed with cummax function
result = [result{:}]; %// concatenate
result(ind) = result; %// undo sorting