Problem 60994. Cumulative Count
Given a row vector of integers with repeating values (with all repeating values occuring in a single continuous run), create an array that is the cumulative count of the unique values -
%% Example 1
input = [4 5 5 6 11 11 11 11 20 20 20 20 20 20 20 35 50 50];
output = [1 1 2 1 1 2 3 4 1 2 3 4 5 6 7 1 1 2];
%% Example 2
input = [33 33 33 22 22 11];
output = [1 2 3 1 2 1];
Only vectorized solutions will be accepted. Check the test suite for banned functions.
Solution Stats
Problem Comments
-
3 Comments
Christian Schröder
on 5 Sep 2025 at 13:17
Cool problem! For the benefit of those who have yet to solve it, you don't need to "remember" the counts of each element, so [4 5 5 6 5 5] should yield [1 1 2 1 1 2] and not [1 1 2 1 3 4].
Dyuman Joshi
on 5 Sep 2025 at 15:25
Thank you Christian!
FYI - Such cases aren't included in the scope of this problem.
I have updated the Problem description to make the statement more precise.
Though they will be considered for upcoming variations of this problem!
Christian Schröder
on 5 Sep 2025 at 19:50
Thanks, Duyman. I have a solution for that case too. Unfortunately submitting problems to Cody appears to be broken right now, which I only realized AFTER creating a new one.
Solution Comments
Show commentsProblem Recent Solvers6
Suggested Problems
-
6 Solvers
More from this Author36
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!