Nested manipulator in math

Please consider:

Function[subID, pointSO[subID] = RandomInteger[{1, 4}, {5, 2}]] /@ {"subA", "subB"}; Manipulate[ Manipulate[ Graphics[{ Black, Rectangle[{0, 0}, {5, 5}], White, Point@pointSO [subID][[i]] }, ImageSize -> {400, 300}], {i,Range[ Length@pointSO [subID]]}], {subID, {"subA", "subB"}}] 

enter image description here

Given that pointSO[subID] does result in lists of different lengths, is there a way to avoid having 2 Manipulate , given that one of the managed variables depends on the other?

+6
source share
3 answers

I'm not sure that I have exactly what you are asking for, but I thought you want something like the following:

Given a user interface with one variable, let's say an array, which can vary in size, and another (dependent) variable, which is an index into the current array that you want to use from the user interface, for indexing into an array.

But you do not want to fix the layout of the index variable in the user interface, because at runtime it depends on the size of the array, which can be changed using the second variable.

Here is one manipulator that has a user interface that has an index control variable that is dynamically updated in the user interface as the size of the array changes.

I used SetterBar for the index (dependent variable), but you can also use the slider. SetterBar has made the user interface that is changing more understandable.

When you change the length of the array, the index control variable automatically updates its maximum allowed index, which will be used to match the current length of the array.

When the array is compressed, the index will also decrease.

I'm not sure if this is what you want, but if it is, you can customize this approach to fit into your problem.

 Manipulate[ Grid[{ {Style[Row[{"data[[", i, "]]=", data[[i]]}], 12]}, {MatrixForm[data], SpanFromLeft} }, Alignment -> Left, Spacings -> {0, 1} ], Dynamic@Grid [{ {Text["select index into the array = "], SetterBar[Dynamic[i, {i = #} &], Range[1, Length[data]], ImageSize -> Tiny, ContinuousAction -> False] }, { Text["select how long an array to build = "], Manipulator[ Dynamic[n, {n = #; If[i > n, i = n]; data = Table[RandomReal[], {n}]} &], {1, 10, 1}, ImageSize -> Tiny, ContinuousAction -> False] , Text[Length[data]], SpanFromLeft } }, Alignment -> Left ], {{n, 2}, None}, {{i, 2}, None}, {{data, Table[RandomReal[], {2}]}, None}, TrackedSymbols -> {n, i} ] 

enter image description here

enter image description here

update 8:30 pm fyi, just fixed the code above to add the necessary additional logic.

+5
source

To avoid a problem with i that is too large when switching lists, you could add an If[] operator at the beginning of Manipulate , for example

 Clear[pointSO]; MapThread[(pointSO[#] = RandomInteger[{1, 4}, {#2, 2}]) &, {{"subA", "subB"}, {5, 7}}]; Manipulate[ If[i > Length[pointSO[subID]], i = Length[pointSO[subID]]]; Graphics[{Black, Rectangle[{0, 0}, {5, 5}], White, Point@pointSO [subID][[i]]}, ImageSize -> {400, 300}], {{subID, "subA"}, {"subA", "subB"}, SetterBar}, {{i, {}}, Range[ Length@pointSO [subID]], SetterBar}] 

It might be better to switch between reset i lists. This can be done by doing something like

 Manipulate[ Graphics[{Black, Rectangle[{0, 0}, {5, 5}], White, Point@pointSO [subID][[i]]}, ImageSize -> {400, 300}], {{subID, "subA"}, SetterBar[Dynamic[subID, (i = {}; subID = #) &], {"subA", "subB"}] &}, {{i, {}}, Range[ Length@pointSO [subID]], SetterBar} ] 
+2
source

An alternative implementation that saves the selection settings for each data set:

 listlength["subA"] = 5; listlength["subB"] = 9; Function[subID, pointSO[subID] = RandomInteger[{1, 4}, {listlength[subID], 2}]] /@ {"subA", "subB"}; Manipulate[ Graphics[{Black, Rectangle[{0, 0}, {5, 5}], Dynamic[If[subID == "subA", Yellow, Cyan]], PointSize -> .05, Dynamic@Point @pointSO[subID][[k]]}, ImageSize -> {400, 300}], Row[{Panel[ SetterBar[ Dynamic[subID, (subID = #; k = If[subID == "subA", j, i]) &],{"subA", "subB"}, Appearance -> "Button", Background -> GrayLevel[.8]]], " ", PaneSelector[{"subA" -> Dynamic@Panel [ SetterBar[Dynamic[j, (k = j; j = #) &], Range[ Length@pointSO ["subA"]], Appearance -> "Button", Background -> Yellow]], "subB" -> Dynamic@Panel [ SetterBar[Dynamic[i, (k = i; i = #) &], Range[ Length@pointSO ["subB"]], Appearance -> "Button", Background -> Cyan]]}, Dynamic[subID]]}]] 

Output Examples:

data set subB

data set subA

+2
source

Source: https://habr.com/ru/post/904457/


All Articles