Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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

This question is related to this quesion. The goal is to use ParallelMap to map a function to a nested Table to get the FinestGrained distribution of calculation. But after that, I want to recover the original nested data structure instead of a flat one.

For example, say I have a nested Table like this

 Table[{line, honeycombnum, distance}, {line, 1, 
   honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}]

as you can see, this nested table structure is not of equal length .

Then what is the general way to recover the data structure after using

ParallelMap[f[#[[1]], #[[2]], #[[3]]] &, Flatten[Table[
   Table[{line, honeycombnum, distance}, {line, 1, 
     honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}], 2], Method -> "FinestGrained"]

By the word "general ", I mean the method should be suitable for arbitrary complex Table structure.

share|improve this question
up vote 3 down vote accepted

A possibility is the following:

it = Flatten@MapIndexed[#2 -> #1 &, t, {-2}];
if[a_ -> b_] := a -> f[b];
res = ParallelMap[if, it];
ReplacePart[t, res]

It adds to the table the index of every object, it creates a new function working like yours but preserving the indices, operates with parallel map and then substitutes all the parts in the original table with the results.

I think this is rather general and perhaps rather terse.

share|improve this answer
Thank you! But I can't understand. Can you explain a little? What is t,it,if? – matheorem Nov 10 '13 at 10:01
Oh, I understand – matheorem Nov 10 '13 at 10:07
Yes, I am sorry for the excessive synthesis...: "t" is the table, "it" is the indexed table, "if" is the index-preserving function based on f. The final line produces the result – user8074 Nov 10 '13 at 19:04

I'm sure there should be an easier way. Meanwhile:

orig = Table[ Table[{line, honeycombnum, distance}, {line, 1, honeycombnum + 1}, 
                                                    {distance, 1, 4}], {honeycombnum, 2, 5}];
(* Form a template for the re-structure op, some pattern to match your data needed*)
i = 0; orig1 = orig /. {_Integer, _Integer, _Integer} :> x[++i] 
(* Let's see how it looks when flattened *)
orig2 = Flatten[orig1]
dest = Flatten[orig /. {a_Integer, b_, c_} -> {a + b + c}] (*Something like your ParallelMap *)
dest1 = orig1 /. Thread[orig2 -> dest] (* The re-structured result*)

Note that you need a pattern to match your data for this method to work

share|improve this answer

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.