Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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


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


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.

share|improve this question
up vote 5 down vote accepted

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*)
 list[[j, k]],
 {j, 1, 2, 1, Manipulator[Dynamic[j, (k = 1; j = #) &], ##2] &},
 {k, 1, Length[list[[j]]], 1}]


An alternative is to completely specify the control:

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


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

share|improve this answer
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? – mikeonly Aug 6 '13 at 21:27
# 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 – Michael E2 Aug 6 '13 at 21:40
@mikeonly I added a couple of alternatives. The first shows what ##2 becomes in this case, namely the single argument {1, 2, 1}. – Michael E2 Aug 7 '13 at 11:51

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}};
  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.

share|improve this answer
So..! I found more one Brazilian guy using MMA. Nice! – Murta Aug 6 '13 at 21:30
@Murta Don't worry, you're not alone... :) – R. M. Aug 6 '13 at 21:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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