开发者

For loop to change the values of a four dimensional table

I would like your help on something,

I have a Table:

InitialMatrix[x_, y_, age_, disease_] :=

  ReplacePart[Table[Floor[Divide[dogpopulation/cellsno,9]], {x}, {y}, {age}, {disease}], 
{{_, _, 1, _} ->  0, {_, _, 3, _} -> 6}];

I was trying to set up a condition to change all the values inside the table to sumthing else, according to a value, I tried:

listInitial={};

For[a = 1, a < 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 it only changes the last part of my table, finally I decided to use the following code instead of the for 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 now I want to change specific parts inside the table, for example I want all the part {__,__,3,_} to change I tried to choose the range with MapAt but again I think I need to do a loop, and I cannot, can any开发者_开发问答 one please help me?

For[x = 1, x < 4, x++,
  listab[MapAt[f, InitialMatrix[3, 3, 3, 3], {x, 3, 3}]//TableForm]]


If you check out the documentation for MapAt, you will see that you can address multiple elements at various depths of your tensor, using various settings of the third argument. Note also the use of Flatten's second argument. I think this is what you are 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 this seems to be your second attempt to ask a question involving a really complicated For loop, may I just emphasise that you almost never need a For or Do loop in Mathematica in the circumstances where you would use one in, say, Fortran or C. Certainly not for most construction of lists. Table works. So do things like Listable functions (which I know you know) and commands like NestList, FoldList and Array.

You will probably also find this tutorial useful. http://reference.wolfram.com/mathematica/tutorial/SelectingPartsOfExpressionsWithFunctions.html


I used the following code as an answer, I am not sure whether is the best solution or not, but it works!!

InitialTable[x_, y_, z_, w_] := 

  MapAt[g,ReplacePart[
   InitialMatrix[3, 3, 3, 3] + 
    ReplacePart[
     Table[If[RandomReal[] > psurvival, -1, 
       0], {3}, {3}, {3}, {3}], {{_, _, 1, _} -> 0, {_, _, 2, _} -> 
       0}], {{_, _, 1, 2} -> 0, {_, _, 1, 3} -> 0}], 
  Flatten[Table[{i, j, 3, l}, {i, x}, {j, y}, {l, w}], 2]];

g[x_] := If[x < 0, 0, x];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜