For循环改变四维表的值 [英] For loop to change the values of a four dimensional table

查看:24
本文介绍了For循环改变四维表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助,

我有一张桌子:

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]]]]]]]

但它只改变了我表的最后一部分,最后我决定使用下面的代码而不是 for 循环,

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

但现在我想更改表格内的特定部分,例如我想要所有部分{__,__,3,_} 改变我试图用 MapAt 选择范围,但我想我需要做一个循环,我不能,可以任何一个请帮帮我?

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]]

推荐答案

如果您查看 MapAt 的文档,您将看到您可以使用第三个参数的各种设置来处理张量不同深度的多个元素.还要注意 Flatten 的第二个参数的使用.我想这就是你要找的.

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.htmlhttp://reference.wolfram.com/mathematica/ref/Flatten.html

http://reference.wolfram.com/mathematica/ref/MapAt.html http://reference.wolfram.com/mathematica/ref/Flatten.html

因为这似乎是您第二次尝试提出涉及非常复杂的 For 循环的问题,所以我可以强调一下,您在 Mathematica 中几乎不需要 For 或 Do 循环会在 Fortran 或 C 中使用一个.当然不是对于大多数列表构造.表工作.Listable 函数(我知道你知道)和 NestList、FoldList 和 Array 等命令也是如此.

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.

您可能还会发现本教程很有用.http://reference.wolfram.com/mathematica/tutorial/SelectingPartsOfExpressionsWithFunctions.html

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

这篇关于For循环改变四维表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆