# make a slider for a list of matrices

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.

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

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

• it works perfect. You and kglr saved my day... – Tugrul Temel Oct 3 '18 at 11:53

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.)

• Your answer is yet another very good answer, formatting the output in MatrixForm for better visual effect. – Tugrul Temel Oct 3 '18 at 12:03
• I am having trouble in Grid, but I cannot figure it out where the cause of the trouble is. – Tugrul Temel Oct 3 '18 at 12:07
• @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}}] ]] – Michael E2 Oct 3 '18 at 14:08
• 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. – Tugrul Temel Oct 3 '18 at 15:23
• @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. – Michael E2 Oct 3 '18 at 23:12