Where is the 0=1 bug coming from in this Mathematica code?

77 views Asked by At

I am trying to build a simple voter model. I need to build a matrix whose values are given by a function. Then aggregate them (in this case simple average) to determine the result (simple 1 or 0). Then determine the deviance of the result from the average. I am stuck because whenever there is a 0 resulting from my if statement it is treated as a 1 by the subtraction. Please help. *Bonus: if there is a way to update a variable for every fourth row of the matrix and have that value carry over to better simulate and election it would be much appreciated. Thank you in advance, Charles

m = 3
n = 5
p := RandomInteger[]
c = Table[p, {i, m}, {j, n}]
t = Total[c, {2}] 
avg = t/n
Function[If[0.5 <= # <= 1, v = 1, v = 0]] /@ avg
d = Abs[v - avg]
Sum[d, i]
1

There are 1 answers

1
Chris Degnen On BEST ANSWER

Replace last three lines:

...
v = Function[If[0.5 <= # <= 1, 1, 0]] /@ avg
d = Abs[v - avg] 
Total[d]