Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

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

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]

Thank you very much!!

Edit: Changed requirements

I have a four dimensional table and I want to change the values in it with respect to 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 am trying to change the first 2 rows in each subtmatrix according to one random variable each time.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
f[mylist_List, rowPlus_Integer, rowMinus_Integer] := 
  Module[{temp = mylist},
    If[1 <= rowPlus <= Length@temp && 1 <= rowMinus <= Length@temp,
      If[ RandomChoice[{True, False}], 
        temp[[rowPlus]]++;
        temp[[rowMinus]]--];
      Return@temp,
    (*else*)
       Print@"Wrong row value"; Abort[]]]

a = IdentityMatrix[3]
a = f[a, 1, 2]
(*
{{2, 1, 1}, {-1, 0, -1}, {0, 0, 1}}
*)
a = IdentityMatrix[3]
a = f[a, 1, 2]
(*
{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}
*)

Edit

According to your new specifications:

f[mylist_List, rowPlus_Integer, rowMinus_Integer, cutoff_Real] :=
 Module[{temp = mylist},
  If[1 <= rowPlus <= Length@temp && 1 <= rowMinus <= Length@temp,
   MapIndexed[
    If[#1 > cutoff,
      temp[[rowPlus, First@#2]]++;
      temp[[rowMinus, First@#2]]--] &,
    RandomReal[1, Dimensions@temp[[2]]]];
   Return@temp,
   (*else*)
   Print@"Wrong row value"; Abort[]]]

Usage

a = IdentityMatrix[3];
a = f[a, 1, 2, .5]
(*
->{{2,1,0},{-1,0,0},{0,0,1}}
*)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...