Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
1  
You might want to look at either of FrobeniusSolve[] or IntegerPartitions[] for generating the indices that can be fed into m. –  J. M. Apr 22 '13 at 23:09

3 Answers 3

up vote 3 down vote accepted

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]

1

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

share|improve this answer
    
this seems to work very well. Thanks a lot!!! –  user7058 Apr 23 '13 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.

ListPlot[tab]
share|improve this answer

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 /@ 
   Transpose[{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

share|improve this answer

Your Answer

 
discard

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.