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!