I have an initial list which is partitioned as follows.
data = {1, 2, 3, 4, 5, 6, 7, 8} part = {{1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, 8}, {7, 8}} The partition puts element i of data in a sublist {i-1,i,i+1}, e.g. the fourth element in data (4) appears in the third, fourth and fifth elements in part in positions 3,2,1 respectively. I want to cursor across part and if a test suceeds I want to replace
1) the ith element in data 2) the elements in part that correspond to it
This code does what I want, with one error that does not affect me.
list := {1, 2, 3, 4, 5, 6, 7, 8} part := {{1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, 8}, {7, 8}} func[{a_, b_, c_}] := (a + b + c)/3 func[{a_, b_}] := (a + b)/2 func[{b_, c_}] := (b + c)/2 Do[{Print[part[[i]]]; test = RandomVariate[NormalDistribution[func[part[[i]]], 1]]; If[list[[i]] > test, {list = ReplacePart[list, i -> test]; part = ReplacePart[part, {{i - 1, 3} -> test, {i, 2} -> test, {i + 1, 1}-> test}]}, Nothing]; }, {i, 1, 8}] part list To make things as clear as possible, I print the input to test and in the end I call the output. I use SetDelayed to store the updated lists in each step. The Do loop does not break for i=1, i=8, which is very useful.
{1,2}
{-0.149498,2,3}
{2,3,4}
{3,4,5}
{3.64625,5,6}
{3.69681,6,7}
{6,7,8}
{7,8}
(part output, as intended)
{{1, -0.149498}, {-0.149498, 2, 3}, {2, 3, 3.64625}, {3, 3.64625, 3.69681}, {3.64625, 3.69681, 6}, {3.69681, 6, 7}, {6, 7, 7.7758}, {7, 7.7758}}
(list output, as intended)
{-0.149498, 2, 3, 3.64625, 3.69681, 6, 7, 7.7758}
The error is in the first element of part, which should be {-0.149498,2}. Since I won't be needing this I can live with the issue. The last element is updated correctly, on the other hand.
I keep the question about efficiency open. Transpose does not cover me because part must be updated in parallel with list (one step at a time), and Table constructs a new list every time which is time consuming (I use ReplacePart to avoid that). Because ReplacePart is slow, possible candidates are MapAt/ Apply, or even NestList.
Again, thank you all for your patience and good will. Hopefully this explains what I am aiming for, which is a dynamic sequence.