Skip to main content
2 of 4
added 923 characters in body
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

It looks like I'm late to the party but here is my approach. Common functions and data:

trinarize[a_List, γ_?NumericQ] := UnitStep[a - γ] + UnitStep[γ + a]
labels = {0 -> "bottom", 1 -> "middle", 2 -> "top"};

SeedRandom[1]
sampleSimulation = RandomChoice[{-1, 0, 1}, 20]
{0, -1, 0, 0, -1, -1, -1, 0, -1, -1, -1, -1, 1, -1, 0, 1, -1, -1, 0, 0}

###Method #1

Split @ trinarize[sampleSimulation, 0.9]  (* 0.9 is γ *)

Split[%, Abs[#[[1]] - #2[[1]]] < 2 &]

Join @@ Riffle[%, {{"unoccupied middle"}}]

% /. p : {x_ ..} :> {x /. labels, Length@p}
{{1}, {0}, {1, 1}, {0, 0, 0}, {1}, {0, 0, 0, 0}, {2}, {0}, {1}, {2}, {0, 0}, {1, 1}}

{{{1}, {0}, {1, 1}, {0, 0, 0}, {1}, {0, 0, 0, 0}}, {{2}}, {{0}, {1}, {2}}, {{0, 0}, {1, 
   1}}}

{{1}, {0}, {1, 1}, {0, 0, 0}, {1}, {0, 0, 0, 0}, "unoccupied middle", {2},
  "unoccupied middle", {0}, {1}, {2}, "unoccupied middle", {0, 0}, {1, 1}}

{{"middle", 1}, {"bottom", 1}, {"middle", 2}, {"bottom", 3}, {"middle", 1}, {"bottom", 
  4}, "unoccupied middle", {"top", 1}, "unoccupied middle", {"bottom", 1}, {"middle", 
  1}, {"top", 1}, "unoccupied middle", {"bottom", 2}, {"middle", 2}}

###Method #2

s1 = Split @ trinarize[sampleSimulation, 0.9]  (* 0.9 is γ *)
s2 = {First @ # /. labels, Length @ #} & /@ s1
n = Length @ s1;
jumps = SparseArray[Unitize[Abs@Differences@s1[[All, 1]] - 1]]["AdjacencyLists"]

Clip[Ordering @ Join[Range@n, jumps], {1, n}, {1, n + 1}];
Append[s2, "unoccupied middle"][[%]]
 
{{1}, {0}, {1, 1}, {0, 0, 0}, {1}, {0, 0, 0, 0}, {2}, {0}, {1}, {2}, {0, 0}, {1, 1}}

{{"middle", 1}, {"bottom", 1}, {"middle", 2}, {"bottom", 3}, {"middle", 1}, {"bottom", 
  4}, {"top", 1}, {"bottom", 1}, {"middle", 1}, {"top", 1}, {"bottom", 2}, {"middle", 2}}

{6, 7, 10}

{{"middle", 1}, {"bottom", 1}, {"middle", 2}, {"bottom", 3}, {"middle", 1}, {"bottom", 
  4}, "unoccupied middle", {"top", 1}, "unoccupied middle", {"bottom", 1}, {"middle", 
  1}, {"top", 1}, "unoccupied middle", {"bottom", 2}, {"middle", 2}}
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k