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
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
Jiri Zurek 2018년 5월 19일
편집: Jiri Zurek 2018년 5월 19일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 5월 19일

편집:

2018년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by