5
$\begingroup$

Context:

There is a list of lists list where lists are different length. Using one control I change a part of list to view, and with another one — what certain part of part to show at the end. Thought, Length could define here number of values in chosen part of list.

list = {{1, 2, 3, 4}, {1, 2, 3}}; (* for example *)
Manipulate[list[[j, k]], {j, 1, 2, 1}, {k, 1, Length[list[[j]]], 1}]

Problem:

When control of j-th part switches to part of list with k-values > then it was before, it leads to a mistake, though when switching in opposite direction slider disposes itself correctly:

Mistake in an action

Question:

Do I desire that from Length in vain, and what is the solution of this problem? Assume, I can somehow set a value of k to 1, when j changes. How can I achieve this?

Thanks. I apologize for that the title not corresponding to an issue fully. Had no idea how to name it.

$\endgroup$

2 Answers 2

7
$\begingroup$

You can reset k whenever j changes if you customize the controller for it a little. Use the second argument to Dynamic to set k to 1 (or anything else, for that matter):

list = {{1, 2, 3, 4}, {1, 2, 3}}; (*for example*)
Manipulate[
 list[[j, k]],
 {j, 1, 2, 1, Manipulator[Dynamic[j, (k = 1; j = #) &], ##2] &},
 {k, 1, Length[list[[j]]], 1}]

Edit

An alternative is to completely specify the control:

{j, 1, 2, 1, Manipulator[Dynamic[j, (k = 1; j = #) &], {1, 2, 1}] &}

or

{{j, 1}, Manipulator[Dynamic[j, (k = 1; j = #) &], {1, 2, 1}] &}

The difference is minimal (it affects Autorun). The first is exactly equivalent to original answer, except that to alter the domain 1, 2, 1, there are two places to edit.

$\endgroup$
3
  • $\begingroup$ Thanks! Your answer fits great. I vaguely understand meaning of # in Dynamic[j, (k = 1; j = #) &], but can't get sense of ##2 then. As I haven't completely grasped pure functions, can you explain that, please? $\endgroup$
    – mikeonly
    Commented Aug 6, 2013 at 21:27
  • $\begingroup$ # stands for the first argument (in the Function in Dynamic) and ##2 stands for arguments 2 and above (in the Function Manipulator[..] &). See this answer for links to the funny symbols, and all the tutorials at the bottom of the Manipulate page for the rest, esp. this part $\endgroup$
    – Michael E2
    Commented Aug 6, 2013 at 21:40
  • $\begingroup$ @mikeonly I added a couple of alternatives. The first shows what ##2 becomes in this case, namely the single argument {1, 2, 1}. $\endgroup$
    – Michael E2
    Commented Aug 7, 2013 at 11:51
2
$\begingroup$

In this specific case, I prefer to keep both sliders' positions but instead of using directly their values, we can check if they do make sense and use a "reasonable guess" when they don't.

So, if k>Length[list[[j]]] instead of changing k into 1, we can use Length[list[[j]]]. I like the collateral effect of not losing the value of k in case we change j back into a value compatible with it. And I prefer to choose the last element when the index is greater than the length of the list than to choose the first.

This way, my 2 cents guess into your problem would be the following:

list = {{3, 5, 1, 7}, {8, 6}, {0}, {2, 9, 4}};
Manipulate[
  Style[list[[j, Min[k, Length[list[[j]]]]]], 
      If[k > Length[list[[j]]], Red, "Output"]],
  {j, 1, Length[list], 1},
  {k, 1, Max[Length /@ list], 1}
]

As I am not using directly the k value chosen by the user, I prefer to change the color but this is only an idea.

$\endgroup$
2
  • $\begingroup$ So..! I found more one Brazilian guy using MMA. Nice! $\endgroup$
    – Murta
    Commented Aug 6, 2013 at 21:30
  • $\begingroup$ @Murta Don't worry, you're not alone... :) $\endgroup$
    – rm -rf
    Commented Aug 6, 2013 at 21:59

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.