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!