wolfram mathematica - For loop to change the values of a four dimensional table -
i on something,
i have table:
initialmatrix[x_, y_, age_, disease_] := replacepart[table[floor[divide[dogpopulation/cellsno,9]], {x}, {y}, {age}, {disease}], {{_, _, 1, _} -> 0, {_, _, 3, _} -> 6}];
i trying set condition change values inside table sumthing else, according value, tried:
listinitial={}; for[a = 1, < 4, a++, for[b = 1, b < 4, b++, for[x = 1, x < 4, x = x + 1, for[z = 1, z < 4, z = z + 1, listinitial = if[random[] > psurvival, replacepart[ initialmatrix[3, 3, 3, 3], {a, b, x, z} -> initialmatrix[3, 3, 3, 3][[a]][[b]][[x]][[z]] - 1], initialmatrix[3, 3, 3, 3], {a, b, x, z} -> initialmatrix[3, 3, 3, 3][[a]][[b]][[x]][[z]]]]]]]
but changes last part of table, decided use following code instead of loop,
setattributes[myfunction, listable] myfunction[x_] := if[random[] > psurvival, if [x - 1 < 0 , x , x - 1], x] myfunction[initialmatrix[3, 3, 3, 3]] // tableform
but want change specific parts inside table, example want part {__,__,3,_}
change tried choose range mapat
again think need loop, , cannot, can 1 please me?
for[x = 1, x < 4, x++, listab[mapat[f, initialmatrix[3, 3, 3, 3], {x, 3, 3}]//tableform]]
if check out documentation mapat, see can address multiple elements @ various depths of tensor, using various settings of third argument. note use of flatten's second argument. think looking for.
mapat[g, initialmatrix[3, 3, 3, 3], flatten[table[{i, j, 3, k}, {i, 3}, {j, 3}, {k, 3}], 2]]
http://reference.wolfram.com/mathematica/ref/mapat.html http://reference.wolfram.com/mathematica/ref/flatten.html
since seems second attempt ask question involving complicated loop, may emphasise almost never need or loop in mathematica in circumstances use 1 in, say, fortran or c. not construction of lists. table works. things listable functions (which know know) , commands nestlist, foldlist , array.
you find tutorial useful. http://reference.wolfram.com/mathematica/tutorial/selectingpartsofexpressionswithfunctions.html
Comments
Post a Comment