So I'm writing some code to try and produce a cellular automaton which will model an oscillatory chemical reaction, but I'm trying to set up the initial conditions of the grid so that I can vary them to see how they change the behavior.

Basically my method has been to use Table to generate a series of nested lists which give the x and y coordinates of the bottom left corner of a grid square, and also attached to those coordinates is a value which can run from 0 to some integer - it is this number that I will be updating to make the automaton evolve. So I essentially have a list that looks like;


But what I want to do is to specify a list of initial conditions, so that the initial domain might look something more like;


Just for argument's sake.

I've tried a few different ways of doing this - using replacement rules and whatnot, but none of it has worked. Does anybody have any suggestions?


Here are two more ways.

lst = {{0, 0}, {1, 0}, {2, 0}};
init = {7, 3, 9};
MapThread[{#1[[1]], #1[[2]], #2} &, {lst, init}]


helper[{a_, b_}, c_] := {a, b, c}
Thread[helper[lst, init]]

Both produce

{{0, 0, 7}, {1, 0, 3}, {2, 0, 9}}

P.S. Just for fun and as my entry into this month's obfuscated Mathematica contest:

Inner[Append, Hold @@ lst, Hold @@ init, List]
  • $\begingroup$ I like "...obfuscated Mathematica contest..." : Append @@@ Transpose[{st, init}] is my entry (late comer handicap though)...apolpgies if it is already somewhere $\endgroup$ – ubpdqn Feb 24 '16 at 6:24

There are many ways to do this. For instance,

lst = {{0, 0}, {1, 0}, {2, 0}};
init = {7, 3, 9};
MapThread[Append[#1, #2] &, {lst, init}]
(* {{0, 0, 7}, {1, 0, 3}, {2, 0, 9}} *)


lst1 = {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}};
MapThread[ReplacePart[#1, 3 -> #2] &, {lst1, init}]

which produces the same result. See Adding, Removing, and Modifying List Elements for a discussion of this topic.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.