I'm trying to construct a large table where new entries depend on the most recent entries. It's quite slow: takes 2 mins and I'm embedding this into a function that does it repeatedly so any reduction in time is a big savings. I think this issue is with "AppendTo" and I've seen repeated references on here to use Reap/Sow, but I'm having trouble implementing that because the next entries in the table are conditional on the previous. Here is an example of the code I'm working with right now.
SeedRandom[1234];
TransMark = Table[RandomReal[], {i, 7}, {j, 7}]; (*Just an example of a similar matrix to what I have*)
RandDraw = Table[RandomReal[], {i, 10000}, {j, 576}];
WorkerMat = Table[{{2, 1}}, {10000}];
Timing[Do[
If[RandDraw[[i, j]] <=
TransMark[[All, 1]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 1,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {1, Tenure}],
If[RandDraw[[i, j]] <=
TransMark[[All, 2]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 2,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {2, Tenure}],
If[RandDraw[[i, j]] <=
TransMark[[All, 3]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 3,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {3, Tenure}],
If[RandDraw[[i, j]] <=
TransMark[[All, 4]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 4,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {4, Tenure}],
If[
RandDraw[[i, j]] <=
TransMark[[All, 5]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 5,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {5, Tenure}],
If[
RandDraw[[i, j]] <=
TransMark[[All, 6]][[Last[WorkerMat[[i]]][[1]]]],
If[Last[WorkerMat[[i]]][[1]] == 6,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {6, Tenure}],
If[Last[WorkerMat[[i]]][[1]] == 7,
Tenure = Last[WorkerMat[[i]]][[2]] + 1, Tenure = 1];
AppendTo[WorkerMat[[i]], {7, Tenure}];]]]]]] , {j, 1,
576}, {i, 1, 10000}];
];
Which takes over 2 mins on my machine. My apologies if this is a repeat question, but I've been looking for a similar post for many hours and have not discovered one. Thanks in advance!
NestList
,FoldList
and their "See Also"s are probably good places to start looking. $\endgroup$