| Original Message (ID '43230') By yehuda ben-shimol: |
| I'm not aware of a single function that would do that, but the logic of what you describe is as follows:
1. Collect all adjacent elements that are apart at most by 1 - you do that with Split and using the second argument
2. Map a function that would replace each of the elements by their average
(This means that the first two elements of your program need to be 3/2 rather then 1/2)
Functional Programming style:
Flatten[Table[#[[1]]/#[[2]], {#[[2]]}] & /@ ({Total[#], Length[#]} & /@
Split[{1, 2, 5, 6, 8, 9}, Abs[#1 - #2] <= 1 &])]
Pattern Matching style:
Flatten[Cases[Split[{1, 2, 5, 6, 8, 9}, Abs[#1 - #2] <= 1 &],
x_List :> Table[Mean[x], {Length[x]}]]] |
|