3
$\begingroup$

I would like to build a Manipulate object, for which some variables will be related to corresponding controls. For example, in this simple Manipulate object, if I change any slider control, both variables mu1 and mu2 are recalculated

Manipulate[
 S1 = RandomVariate[NormalDistribution[mu1, 1], 10]; 
 S2 = RandomVariate[NormalDistribution[mu2, 1], 10];
 TableForm[{S1, S2}],  {mu1, -1, 1}, {mu2, -1, 1}]

Is it possible to establish recalculation for each variable separately: when the 1st control is changed, only mu1 is recalculated; when 2nd--- only mu2?

$\endgroup$

2 Answers 2

4
$\begingroup$

Make s1 and s2 manipulate parameters with no controls and update them using TrackingFunction in controls mu1 and mu2:

Manipulate[Style[#, 16] & @ TableForm[{s1, s2}],
 {mu1, -1, 1, TrackingFunction -> (mu1 = #; 
     s1 = RandomVariate[NormalDistribution[#, 1], 5]; &)},
 {mu2, -1, 1, TrackingFunction -> (mu2 = #; 
     s2 = RandomVariate[NormalDistribution[#, 1], 5]; &)},
 {{s1, RandomVariate[NormalDistribution[-1, 1], 5]}, None},
 {{s2, RandomVariate[NormalDistribution[-1, 1], 5] }, None}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Yours works a lot better than mine does, this is definitely the correct solution to the question. $\endgroup$
    – Robjobbob
    Dec 22, 2020 at 14:13
1
$\begingroup$

By using Dynamic[] we are able to localise variables, preventing the entire expression from updating.

Clear[S1];
Clear[S2];

S1[mu1_] := RandomVariate[NormalDistribution[mu1, 1], 1][[1]];
S2[mu2_] := RandomVariate[NormalDistribution[mu2, 1], 1][[1]];

Manipulate[
 {
   {{Table[Dynamic[S1[mu1]], 10]}},
   {{Table[Dynamic[S2[mu2]], 10]}}
 } // TableForm,
 {mu1, 0, 1},
 {mu2, 0, 1}
]

I see that you have been asking this question for over a year now on various forums, I hope that I helped you today.

$\endgroup$
4
  • $\begingroup$ In addition, it's also usually good practise to use the TrackedSymbols option of Dynamic to specify more precisely when each display element should update. $\endgroup$ Dec 21, 2020 at 16:23
  • $\begingroup$ I hope you helped, but I don't remember about asking this question anywhere $\endgroup$
    – Konstantin
    Dec 21, 2020 at 16:39
  • $\begingroup$ @Konstantin you asked on Wolfram Community 1 year ago community.wolfram.com/groups/-/m/t/1809181 $\endgroup$
    – Robjobbob
    Dec 21, 2020 at 17:06
  • $\begingroup$ Oh, sorry, I forgot this question on Wolfram Community :(( $\endgroup$
    – Konstantin
    Dec 22, 2020 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.