3
$\begingroup$

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;

{{0,0,0},{1,0,0},{2,0,0}...{0,3,0},{1,3,0}...{n,n,0}}

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

{{0,0,7},{1,0,3}...{2,3,9},{3,3,0},{4,3,2}...{n,n,3}}

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?

$\endgroup$
3
$\begingroup$

Here are two more ways.

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

and

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]
$\endgroup$
  • $\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
1
$\begingroup$

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}} *)

or

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.

$\endgroup$

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.