Mathematica Asked on January 19, 2021
Suppose that there are two lists, where capital letters in List1
denote matrices.
List1={A1,A2,A3,A4,A5}
={{{1,2},{3,4}},{{0,1},{3,1}},{{1,1},{2,3}},{{2,1},{5,0}},{{1,1},{2,2}}};
(*ex: List1[[1]]={{1,2},{3,4}} *)
List2={1,2,3,4,5};
Suppose that we have the following code with Manipulate
:
Manipulate[
Plot[Sin[b1 x], {x, 0, 10}],
Row[{Control[{b1, List2, Animator, AnimationRunning -> False}]}]
];
This nicely works. However, applying the same logic to the following code with a list of matrices, List1
, does not work.
Manipulate[
Det[B1*B1],
Row[{Control[{B1, List1, Animator, AnimationRunning -> False}]}]
];
I do not want to use Matrix index such as List1[[k]]
to retrieve k-th sub-matrix because in a large code matrix indices are a bit difficult to use. I just want to apply the same logic that is valid for List2
.
Is there anyway to run the second Manipulate
above for matrices without specifying matrix index?
List1 = {A1, A2, A3, A4, A5} = Array[Symbol["a" <> ToString[#]][##2] &, {5, 2, 2}];
Cheating via the second argument of Dynamic
:
ClearAll[animator]
animator[lst_] := Animator[Dynamic[#, {(b = #; B1 = lst[[#]]) &}], {1, Length@lst, 1},
AnimationRunning -> False] &;
Manipulate[Det[B1 B1], {{b, First@List1, "B1"}, List1, animator[List1]},
Initialization -> {B1 = First[List1]}]
Note: The first two arguments of control, as long as the control is named b
, does not matter. That is, the following gives the same result:
Manipulate[Det[B1 B1], {{b, blah, "B1"}, blahblah, animator[List1]},
Initialization -> {B1 = First[List1]}]
Correct answer by kglr on January 19, 2021
This is an issue that comes into effect when List1
contains 2x2 matrices. The Control
option takes a length 2 list as a 2nd argument, so it's confusing your matrices with that syntax. Note that this does not happen with 3x3 matrices:
A1 := RandomReal[10, {3, 3}]
A2 := RandomReal[10, {3, 3}]
A3 := RandomReal[10, {3, 3}]
A4 := RandomReal[10, {3, 3}]
A5 := RandomReal[10, {3, 3}]
List2 = {A1, A2, A3, A4, A5};
Manipulate[Det[B1*B1],
Row[{Control[{B1, List2, Animator, AnimationRunning -> False}]}]]
In my opinion, the easiest way to avoid this is to just use the Part
operator if you're dealing with a list of 2x2 matrices:
A1 := RandomReal[10, {2, 2}]
A2 := RandomReal[10, {2, 2}]
A3 := RandomReal[10, {2, 2}]
A4 := RandomReal[10, {2, 2}]
A5 := RandomReal[10, {2, 2}]
List2 = {A1, A2, A3, A4, A5};
Manipulate[Det[List2[[B1]]^2],
Row[{Control[{B1, Range[5], Animator, AnimationRunning -> False}]}]]
No doubt there exist other ways, but that's what I do. Sorry , I know you said it was what you wanted to avoid.
Answered by ktm on January 19, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP