Kakashi Hatake
Kakashi Hatake

Reputation: 33

Matlab running max by group

In Matlab, how do I compute the running maximum of an array for each group (labeled by another array subs)? For example, think of the array subs as labels for 3 students, and the corresponding values in val as test scores, I want to compute the the running maximum score achieved by each student.

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

The desired output has the same size as val and gives the current maximum score achieved by that student:

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

My dataset is pretty large, with millions of entries so I would like to avoid using a for-loop. If I just wanted overall the maximum score for each student, I would use accumarray(subs,val,[],@max) but here the problem is more difficult since I want to running-maximum.

There is a similar question in R, but I would like to be able to do this in Matlab. Finding running maximum by group in R

Thanks!

Upvotes: 3

Views: 235

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

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

Upvotes: 4

Related Questions