wolfram mathematica - Change two different parts in a table according to the same random value -


i trying change 2 different rows in table according 1 random variable each time, example want if random number>0.5 change 1 row value in +1 , other row value -1, otherwise nothing. tried following code confused, can please me?

initialmatrix[3, 3, 3, 3] +    mapat[f, mapat[g, table[0, {3}, {3}, {3}, {3}],      flatten[table[{i, j, 1, k}, {i, 3}, {j, 3}, {k, 3}], 2]],     flatten[table[{i, j, 2, k}, {i, 3}, {j, 3}, {k, 3}], 2]] // tableform  f[x_] := if[randomreal[] > 0.5, g[x] = if[x > 0, x - 1, x];    if[g[x] > 0, x + 1, x], x] 

thank much!!

edit: changed requirements

i have 4 dimensional table , want change values in respect each other.

my table is

initialmatrix[x_, y_, age_, disease_] :=    replacepart[ table[floor[divide[dogpopulation/cellsno,  9]],      {x}, {y}, {age},  {disease}], {{_, _, 1, _} ->   0, {_, _, 3, _} -> 6}]; 

i trying change first 2 rows in each subtmatrix according 1 random variable each time.

for example want if random number>0.4 change first element in first row value +1 , first element of second row value minus 1 otherwise leave value is.

i want check every element in first row different random number , if condition true change both 1 , second row. can that?

this answer has "i did @ work without mathematica check syntax" advisory on it

@belisarius' answer works well, more elegant, , more efficient mine. might find alternative easier understand using arrayflatten command (documentation) or join command (documentation).

the first option assumes want code in question asks for, rather stated in text. use like:

nonisfunction[mylist_list,firstrowsub_?vectorq,  secondrowsub_?vectorq,cutoff_real]/; length[dimensions[mylist]]==4  := table[ if[randomreal[]>cutoff,      join[{firstrowsub},{secondrowsub},drop[mylist[[i,j]],2] ],     mylist[[i,j]] ],      {i,dimensions[mylist][[1]]}, {j,dimensions[mylist][[2]]} ] 

this takes each submatrix , stitches again, substiting each time 2 rows substituted if cutoff if exceeded, leaving submatrix alone if not.

if want increment , decrement 2 rows (ie text question rather code provided), similar solution need addition, not join. because plus listable, should thread vector added on rows of each submatrix.

nonisfunction[mylist_list,cutoff_real]/; length[dimensions[mylist]]==4  := table[ mylist[[i,j]] +  if[randomreal[]>cutoff, {1,-1,0,0}, {0,0,0,0} ],     {i,dimensions[mylist][[1]]}, {j,dimensions[mylist][[2]]} ] 

this table-based programming style more procedural programming style used (quadruply nested for loops), still more efficient, , have come conclusion more understand code.

edit: additional material in response noni's question in comments run code (taking second example function above example process), first define function copying , pasting above code mathematica notebook. define input matrix , give name. e.g.

myinputmatrix = randominteger[{10,100},{4,4,4,4}]; 

and finally, call nonisfunction appropriate arguments.

nonisfunction[myinputmatrix,0.4] 

and if doesn't help, recommend video tutorials http://www.wolfram.com/support/learn/get-started-with-mathematica/.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -