I have implemented this function which smooths the values in a list by, if it encounters a zero, replacing this entry by the average of its two neighbours (apart from the first and last values of the list, which are left alone). The following works:
smoother[vTemp__] :=
Module[{vTempSmoothed = ConstantArray[-1, Length[vTemp]], i},
For[i = 1, i <= Length[vTemp], i++,
If[And[i > 1, i < Length[vTemp]],
If[vTemp[[i]] == 0,
vTempSmoothed[[i]] = Mean[{vTempSmoothed[[i - 1]], vTemp[[i + 1]]}],
vTempSmoothed[[i]] = vTemp[[i]]],
vTempSmoothed[[i]] = vTemp[[i]]]]; vTempSmoothed]
Testing the following on the list below returns the required result:
vTempTest = {1, 3, 4, 6, 8, 0, 11, 12, 0, 13};
smoother[vTempTest]
However, wanting to improve my knowledge of functional programming, I would like to know how to replace the above For loop with a more functionally-desirable alternative (for example Map
, or Apply
).
Does any one have an idea how I could do this?