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
-
6 Comments
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...
There are reasonable situations that are not addressed by the problem definition or tested for.
IE if there was a case such as x = [5 3 3 1 3 3 3] is the expected answer
r = [1 1 2 1 1 2 3] or
r = [1 1 2 1 2 2 3]?
Solution Comments
Show commentsGroup

ASEE Challenge
- 10 Problems
- 250 Finishers
- Find the biggest empty box
- How long is the longest prime diagonal?
- Flag largest magnitude swings as they occur
- Solitaire Cipher
- Spot the outlier
- How long is the longest prime diagonal?
- Solitaire Cipher
- Implement simple rotation cypher
- Given a window, how many subsets of a vector sum positive
- How many trades represent all the profit?
- Flag largest magnitude swings as they occur
- Make a run-length companion vector
- Spot the outlier
- Find a subset that divides the vector into equal halves
- How long is the longest prime diagonal?
- Find the biggest empty box
Problem Recent Solvers645
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!