Re: lattice definition: help
- To: mathgroup@smc.vnet.net
- Subject: [mg11247] Re: lattice definition: help
- From: Paul Abbott <paul@physics.uwa.edu.au>
- Date: Wed, 4 Mar 1998 01:39:10 -0500
- Organization: University of Western Australia
- References: <6d0ch7$2no@smc.vnet.net>
Christofer Edling wrote: > I wish to generate an n*n lattice with the following characteristics: > > 1. Each cell in the lattice can be in one out of three possible states, > i.e. A,B,C. > 2. The distribution of states across the whole lattice is defined by > user input, i.e. 30% A's, 20% B's, 50% C's. 3. Given the defined > distribution, each cell in the lattice should be randomly assigned a > state (A,B or C). An answer appeared in the Mathematica Journal 1(3): 57. For symbols In[1]:= symbols = {a, b, c}; with relative frequencies In[2]:= freqs = {0.3, 0.2, 0.5}; we count for how many symbols the cumulative frequencies: In[3]:= cumfreq[l_List] := FoldList[Plus, First[l], Rest[l]]/Plus @@ l; In[4]:= cf = cumfreq[freqs] Out[4]= {0.3, 0.5, 1.} are less than a fixed random number t in the range [0,1], and use the number of hits as the index into the alphabet: In[5]:= index[f_, r_] := Length[Select[f, r >= #1 & ]] + 1; In[6]:= rand[l_List, f_List] := l[[index[f, Random[]]]] In[7]:= Table[rand[symbols, cf], {4}, {4}] Out[7]= {{c, a, c, c}, {c, a, c, c}, {a, a, b, c}, {c, c, c, c}} In 2 Dimensions suppose the "alphabet" In[8]:= s = {a, b, c, d}; t = {1, 2, 3, 4}; has the joint frequency table In[9]:= f = {{0, 0, 0, 3}, {1.2, 0, 2, 0}, {1, 1, 0, 0}, {1, 0, 0.9, 1}}; The cumulative frequencies by row are In[10]:= frow = cumfreq[Plus @@ Transpose[f]] Out[10]= {0.27027, 0.558559, 0.738739, 1.} whilst the set of cumulative frequencies by column are In[11]:= fcol = cumfreq /@ f Out[11]= 1 {{0, 0, 0, 1}, {0.375, 0.375, 1., 1.}, {-, 1, 1, 1}, 2 {0.344828, 0.344828, 0.655172, 1.}} We can use the pair of cumulative frequencies to produce a random sample following the joint frequency table as follows. We use one uniformly distributed random number to fix the row index and, using this index into the cumulative frequencies by column, a second random number to fix the column index: In[12]:= sampmat = Table[{s[[i = index[frow, Random[]]]], t[[index[fcol[[i]], Random[]]]]}, {5}] Out[12]= {{d, 4}, {d, 4}, {d, 4}, {a, 4}, {d, 4}} Cheers, Paul ____________________________________________________________________ Paul Abbott Phone: +61-8-9380-2734 Department of Physics Fax: +61-8-9380-1014 The University of Western Australia Nedlands WA 6907 mailto:paul@physics.uwa.edu.au AUSTRALIA http://www.pd.uwa.edu.au/~paul God IS a weakly left-handed dice player ____________________________________________________________________