Correcting numeric progression in array

Having
A = [4 5 6 6 8]
I need a matlab procedure to "correct" this numeric progression so that there are no two elements of the same value (here 6 twice), but so that result is
B = [4 5 6 7 8]
However, the array is not always linear progression where each next element is the previous element plus one. So, even array like
A = [4 5 6 6 10 10 11]
should return
B = [4 5 6 7 9 10 11]
- "filling the holes" where holes can be found.
I tried with a loop and many if statements in between which check for the adjacent elements, trying to add or subtract 1 where necessary if twice the same number is met, while testing boundaries of the adjacent elements to see whether the new value "fits" in. It is simplified (does not solve all cases), it has about 20 lines and it is slow when the array is long. I am almost sure there is some matlab-wise method which would use vectorizing and similar beautiful features to make this work much faster. Please suggest the optimal solution for this task. Thank you.

2 Comments

What should the output be for 4 5 6 6 8 8? Your rules say that the second 6 has to change to 7, but your rules also say that the first 8 has to change to 7, and that introduces two 7's in a row.
Now what about 4 5 6 6 8 8 9 10 11?
Jiri Zurek
Jiri Zurek on 19 May 2018
Edited: Jiri Zurek on 19 May 2018
The output for 4 5 6 6 8 8 should probably be 4 5 6 7 8 9. The output of 4 5 6 6 8 8 9 10 11 should be error or some disclaimer since this is an anomaly situation that there are not enough integers to form a sequence. So, it cannot be corrected, one should be notified that an anomaly was encountered.

Sign in to comment.

Answers (0)

Categories

Asked:

on 19 May 2018

Edited:

on 19 May 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!