3
$\begingroup$

I have a list of matrices, called $matList$, which are sequentially multiplied by another matrix $G$. I created a slider for $matList$, but the slider does not work. Here is the Code.

Clear[a, t, matList, G, solG, solGTr, mat];
Manipulate[
 SeedRandom[2];
 a = RandomReal[{0, 1}, {3, 5}];
 t = RandomReal[{0, 1}, {3, 5}];
 matList = 
  Table[(# - DiagonalMatrix[Diagonal[#]]) &[
    KroneckerProduct[a[[i]], t[[i]]]], {i, 1, 3}]; 
 G = {{0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {1, 0, 0, 1, 0}, {0, 1, 1, 0, 
    0}, {1, 0, 0, 1, 0}};
 solG = G*mat;
 solGTr = Transpose[solG];
 Grid[{   
   {Text["matrix solG"], Text["transpose of solG"]}, {solG, solGTr}   
       }],
 Row[{Control[{mat, matList, Animator, AnimationRunning -> False}]}]
 ]

The problem in the above Code lies in Row[{control[{mat,matList,...}]. A similar construct for a slider of a list of numbers works, though.

$\endgroup$
3
  • 1
    $\begingroup$ Replace solG = G*mat withsolG = G*matList[[k]], and Row[...] with {{k, 1}, 1, Length@matList, 1, Animator, AnimationRunning -> False}. $\endgroup$
    – kglr
    Oct 3, 2018 at 11:11
  • 1
    $\begingroup$ Related/duplicate: mathematica.stackexchange.com/questions/123152/… $\endgroup$
    – Michael E2
    Oct 3, 2018 at 11:51
  • $\begingroup$ @kglr: Thank you very much for saving my day.... $\endgroup$ Oct 3, 2018 at 11:54

2 Answers 2

2
$\begingroup$

More an extended comment than an answer.

In addition to what kglr has said, you might have much more fun with this if you move the random matrix generation and the preliminary calculations out of the Manipulate environment:

Clear[a, t, matList, G, solG, solGTr, mat];
SeedRandom[2];
n = 30;
a = RandomReal[{0, 1}, {n, 5}];
t = RandomReal[{0, 1}, {n, 5}];
matList = 
  Table[(# - DiagonalMatrix[Diagonal[#]]) &[KroneckerProduct[a[[i]], t[[i]]]], 
   {i, 1, n}];
G = {{0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {1, 0, 0, 1, 0}, {0, 1, 1, 0,0}, {1, 0, 0, 1, 0}};
Manipulate[
 solG = G*matList[[k]];
 solGTr = Transpose[solG];
 Grid[{{Text["matrix solG"], Text["transpose of solG"]}, {solG,solGTr}}], 
 {{k, 1}, 1, Length@matList, 1, Animator, AnimationRunning -> False}
 ]
$\endgroup$
2
  • $\begingroup$ it works perfect. You and kglr saved my day... $\endgroup$ Oct 3, 2018 at 11:53
  • $\begingroup$ Always at your service. =) $\endgroup$ Oct 3, 2018 at 11:57
3
$\begingroup$

Maybe if you want a slider, use Slider:

SeedRandom[2];
a = RandomReal[{0, 1}, {3, 5}];
t = RandomReal[{0, 1}, {3, 5}];
matList = 
  Table[(# - DiagonalMatrix[Diagonal[#]]) &[
    KroneckerProduct[a[[i]], t[[i]]]], {i, 1, 3}];
G = {{0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {1, 0, 0, 1, 0}, {0, 1, 1, 0, 
    0}, {1, 0, 0, 1, 0}};
Manipulate[
 With[{solG = G*mat}, {solGTr = Transpose[solG]},
  Grid[{{Text["matrix solG"], Text["transpose of solG"]},
    MatrixForm /@ {solG, solGTr}}]
  ],
 {mat, matList, Slider}]

If you don't have V10.4 or later or don't mind nested With, you can use the following variant:

Manipulate[
 With[{solG = G*mat}, With[{solGTr = Transpose[solG]},
   Grid[{{Text["matrix solG"], Text["transpose of solG"]},
     MatrixForm /@ {solG, solGTr}}]
  ]],
 {mat, matList, Slider}]

The important thing about With in both variants is that it avoids an extra update by Manipulate that occurs if the global variables solG and solGTr are given new values as they are in the OP. (When these updates are expensive, it can become a critical issue. In the example demo, updating is fast and the waste of time doesn't matter that much.)

$\endgroup$
8
  • $\begingroup$ Your answer is yet another very good answer, formatting the output in MatrixForm for better visual effect. $\endgroup$ Oct 3, 2018 at 12:03
  • $\begingroup$ I am having trouble in Grid, but I cannot figure it out where the cause of the trouble is. $\endgroup$ Oct 3, 2018 at 12:07
  • 1
    $\begingroup$ @Tebernus What sort of trouble? If you have an older version of Mathematica, it might be trouble with With: try With[{solG = G*mat}, With[{solGTr = Transpose[solG]}, Grid[{{Text["matrix solG"], Text["transpose of solG"]}, MatrixForm /@ {solG, solGTr}}] ]] $\endgroup$
    – Michael E2
    Oct 3, 2018 at 14:08
  • $\begingroup$ I have Mathematica 10 and nested With works fine. Apparently, my problem was the old version of Mathematica. My question has been answered . Thank you very much. $\endgroup$ Oct 3, 2018 at 15:23
  • 1
    $\begingroup$ @Tebernus Whenever a "tracked" symbol is updated (with an assignment, such as solG = G*mat, whether it is global or local), Mathematica re-executes Dynamic[]-wrapped code. The body of a Manipulate is such a code. When you have nested Dynamic codes, such as a Manipulate within a Manipulate, things get complicated. There are many questions about it on this site, because it is hard to figure out. I can't give a general answer, but I might be able to respond to a specific code example. Generally, I avoid nesting a Manipulate inside another. There's usually a better way. $\endgroup$
    – Michael E2
    Oct 3, 2018 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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