Correcting numeric progression in array
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 19 de Mayo de 2018
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
el 19 de Mayo de 2018
Editada: Jiri Zurek
el 19 de Mayo de 2018
Respuestas (0)
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!