根据相同的随机值更改表中的两个不同部分 [英] Change two different parts in a table according to the same random value

查看:22
本文介绍了根据相同的随机值更改表中的两个不同部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次都尝试根据一个随机变量更改表中的 2 行不同的行,例如,如果随机数> 0.5,则更改其中一行的值 +1,另一行更改其值值 -1,否则什么都不做.我尝试了以下代码,但我很困惑,有人可以帮我吗?

I am trying to change 2 different rows in my table according to one random variable each time, For example I want if a random number>0.5 then to change one row with the value in it +1 and the other row with its value -1, otherwise do nothing. I tried the following code but I am confused, Can anyone please help 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]

非常感谢!!

更改要求

我有一个四维表,我想相对于彼此更改其中的值.

I have a four dimensional table and I want to change the values in it with respect to each other.

我的桌子是

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

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

我每次都尝试根据一个随机变量更改每个子矩阵中的前 2 行.

I am trying to change the first 2 rows in each subtmatrix according to one random variable each time.

例如,我想要如果一个随机数>0.4,然后将第一行中的第一个元素的值更改为 +1,将第二行的第一个元素的值更改为负 1,否则将值保持原样.

For example I want if a random number>0.4 then to change the first element in the first row with its value +1 and the first element of the second row with its value minus 1 otherwise leave the value as it is.

我想用不同的随机数检查第一行中的每个元素,如果条件为真,则更改第一行和第二行.我可以这样做吗?

I want to check every element in the first row with a different random number and if the condition is true then to change both 1 and second row. Can I do something like that?

推荐答案

这个答案有我在工作时做的,没有 Mathematica 来检查我的语法"咨询

@belisarius 的回答非常有效,更优雅,而且几乎肯定比我的更有效率.但是您可能会发现使用 ArrayFlatten 命令(文档) 或 Join 命令(文档).

@belisarius' answer works perfectly well, is more elegant, and is almost certainly more efficient than mine. But you might find an alternative that is easier for you to understand using the ArrayFlatten command (documentation) or the Join command (documentation).

第一个选项假设您想要类似于问题中的代码要求的内容,而不是文本中所述的内容.然后你会使用类似的东西:

The first option assumes you want something like what the CODE in your question asks for, rather than what is stated in the text. You would then use something 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 effectively takes each submatrix and stitches it back together again, substiting each time the two rows to be substituted if the cutoff if exceeded, but leaving the submatrix alone if it is not.

如果您想要增加和减少两行(即您的文本问题而不是您提供的代码),那么类似的解决方案只需要添加,而不是Join.因为 PlusListable,它应该将要添加到每个子矩阵的行上的向量线程化.

If what you want is to increment and decrement the two rows (ie your text question rather than the code you provided), then a similar solution would simply need addition, not Join. Because Plus is Listable, it should thread the vector to be added over the 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]]} ]

这种基于表的编程风格更像你习惯的过程式编程风格(四层嵌套 For 循环),但它仍然更有效率,我得出的结论是你更有可能理解代码.

This table-based programming style is much more like the procedural programming style you are used to (quadruply nested For loops), but it is still more efficient, and I have come to the conclusion that you are more likely to understand the code.

针对诺丽在评论中提出的问题的补充材料要运行此代码(以上面的第二个示例函数作为此过程的示例),您首先要通过将上述代码复制并粘贴到 Mathematica 笔记本中来定义该函数.然后您将定义您的输入矩阵并为其命名.例如

additional material in response to noni's question in comments To run this code (taking the second example function above as the example for this process), you would first define the function by copying and pasting the above code into a Mathematica notebook. You would then define your input matrix and give it some name. e.g.

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

最后,使用适当的参数调用 nonisFunction.

And finally, call the nonisFunction with the appropriate arguments.

nonisFunction[myinputmatrix,0.4]

如果这没有帮助,我推荐视频教程 http://www.wolfram.com/support/learn/get-started-with-mathematica/.

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

这篇关于根据相同的随机值更改表中的两个不同部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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