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 个评论
Walter Roberson
2018-5-19
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
2018-5-19
编辑:Jiri Zurek
2018-5-19
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!