I have the following problem. I have a table, call it m, which is labelled by 3 indices (say i,j,k), each of them running from 1 up to 100. I would like to be able to construct a new table which would consist of elements taken from m under the following condition:

For any p, such that 2 < p < 301, construct the table consisting of pairs, where the first term in all pairs for a given p is always the same equal p*0.06

Table[{0.06*p, m[[i, j, k]]}, {p, 2, 301}]  WITH THE CONSTRAINT  i + j + k = p

It is the constraint which puzzles me.

I would also like to plot all these Lists on one graph (using ListPlot?). I managed to do something like this for a table with two indices using a Do loop, but for three indices, it seems I would have to use a double loop and I don't know how to do that.

Looking forward to any suggestions!

  • 1
    $\begingroup$ You might want to look at either of FrobeniusSolve[] or IntegerPartitions[] for generating the indices that can be fed into m. $\endgroup$ Apr 22, 2013 at 23:09

3 Answers 3


You could list out the entire table then use GatherBy to reorganise it:

gathered = GatherBy[Flatten[Table[
  {i + j + k, m[[i, j, k]]}, {i, 100}, {j, 100}, {k, 100}], 2], First];

This gathers together all the elements where the first value (the sum of the indices) is the same. You'll have big lists around the sum of the average index value, and small ones at the extremes.

ListPlot[Length /@ gathered]


You can plot a particular list (say p = 150) with ListPlot[Last /@ gathered[[150]]]. But I don't have an interesting m to plot.

  • $\begingroup$ this seems to work very well. Thanks a lot!!! $\endgroup$
    – user7058
    Apr 23, 2013 at 12:17

How about this:

tab = Cases[Flatten[Table[{p == i + j + k, 0.06 p, m[[i,j,k]]}, {i, 100}, {j, 
 100}, {k, 100}, {p, 2, 301}], 3], {True, __}][[;;,2;;]]

Having Flattened the table before finding Cases you should be able to plot this directly.


In my opinion, the new table should have the same dimension as m, since every i,j,k in the range of 1 to 100 satisfy the relationship 2<p<301. So we only need to prepend the sum of the index to the elements in m.

m = Table[RandomReal[{1, 10}], {100^3}];
ls = Flatten /@ 
      Table[{0.06 (i + j + k)}, {i, 1, 100}, {j, 1, 100}, {k, 1, 
        100}], 2], m}];

a list of 10^6 is too large for ListPlot, so I only plot part of it

ListPlot[ls2[[1 ;; -1 ;; 1000]], Joined -> True]

enter image description here


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.