Problem 722. Make a run-length companion vector
Given a vector x, return a vector r that indicates the run length of any value in x. Each element in r shows how many times the corresponding element in x has appeared consecutively.
Example:
Input x = [5 3 3 1 0 9 9 4 4 4 4 5 1 2 2] Output r = [1 1 2 1 1 1 2 1 2 3 4 1 1 1 2]
Solution Stats
Problem Comments
-
5 Comments
The first test case seems to be invalid answer. Please check it
Hi Wenwu: Could you be more specific about what you think the problem is?
See FileExchange's seqle . (disclaimer: I'm the author)
False exemple description
y=[];
for i = 1:length(delta)
test = max(delta(1:i));
maxsofar=max(delta(1:i-1));
test2=min(delta(1:i));
minsofar=min(delta(1:i-1));
if test == delta(i) && delta(i)~= maxsofar
y=[y,1];
elseif test2 == delta(i) && delta(i)~= minsofar
y=[y,-1];
else
y=[y,0];
end
end
this code despite its giving correct results it is rejected by the tests...
Solution Comments
Show commentsProblem Recent Solvers625
Suggested Problems
-
Swap the first and last columns
18819 Solvers
-
Count from 0 to N^M in base N.
234 Solvers
-
Who knows the last digit of pi?
642 Solvers
-
Multiples of a Number in a Given Range
595 Solvers
-
Create matrix of replicated elements
371 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!