How to change consecutive duplicate values, so that they are unique
Show older comments
I have an array A = [1,2,2,4,5,5,5,6], and I would like to get B=[1,2,2.1,4,5,5.1,5.2,6]. How to do it? Thanks!
2 Comments
the cyclist
on 15 Feb 2023
A few questions:
- are the input values guaranteed to be integers?
- are the duplicate values guaranteed to be next to each other?
- are you guaranteed to have 9 or few duplicates of a given number?
Jasmine Zhu
on 15 Feb 2023
Accepted Answer
More Answers (1)
John D'Errico
on 15 Feb 2023
Edited: John D'Errico
on 15 Feb 2023
I once wrote an unrounding tool, that did something like what you want to do. The goal I chose was to perform a minimal perturbation to the original squence that was consistent with having rounded the initial vector, yet is still monotonic, AND is as smooth as possible.
However, you need to consider if your goal is fully valid. It seems you want to add 0.1 to each replicated element. But how would your plan work for the vector
V = [1 1,repmat(2,1,15),3 3 3 3 3 3 3]
The thing is, in the vector V, adding multiples of 0.1 will fail.
Vhat = unround(V)
plot([V;Vhat]','o-')
No perturbation to V is greater than 0.5.
norm(V - round(Vhat))
You could do something different of course.
Edit: Of curse, since now I know that your vector has 3 million elements in it, I would expect that a tool that calls quadprog will fail anyway. For the future, it would have been good of you to tell us pertinent information like that, as I would not have bothered to answer with this solution.
1 Comment
Jasmine Zhu
on 15 Feb 2023
Categories
Find more on Debugging and Improving Code in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!