2
$\begingroup$

A simple problem I am facing is here:

list1 = RandomReal[{1, 2}, {3, 4, 30}];
list2 = RandomReal[{10, 20}, {3, 4, 30}];
Map[Map[(# + 1+Min[#]) &, #] &, #] &/@list1;

works fine, but whenever such a nested Map appears, I think there might be a better solution than what I am doing here. Now my problem is if I want to use a MapThread over the above function like the following, I stumble upon errors

MapThread[Map[Map[(# + 1/Min[#2]) &, #] &, #1] &, {list1, list2}];

Including this example and for even more nested situations, is there any general coding practice that is elegant, efficient and native to MMA functional language paradigm. Hope some one can help me with this type list manipulation here.

$\endgroup$
3
  • $\begingroup$ in your first example list2, does not do much. Could you provide a simple example of the result you'd like. $\endgroup$
    – user21
    Commented Mar 22, 2012 at 11:21
  • $\begingroup$ @ruebenko I dont expect any special result. I just used this example to highlight the situation one faces when using nested Map and question is if there is a way to use MapThread over such nested Map. Feel free to modify the question if you get a better example to make the list2 important for the outer MapThread. $\endgroup$ Commented Mar 22, 2012 at 11:28
  • 1
    $\begingroup$ I think the problem is not nested map, but nested pure functions with &. Try using Function as @ruebenko answer. $\endgroup$
    – FJRA
    Commented Mar 22, 2012 at 13:03

2 Answers 2

4
$\begingroup$

I am not exactly sure what you are looking for so, here are two ideas:

MapThread[
 f[#1, #2] &, {Map[Map[(# + 1/Min[#]) &, #] &, #] & /@ list1, list2}]

or

MapThread[
 Function[{x, y}, Map[Map[(# + 1/Min[y]) &, #] &, x]], {list1, list2}]

Hope this helps.

$\endgroup$
1
  • $\begingroup$ Using Function is the best option to see if every thing is applied when you want. $\endgroup$
    – FJRA
    Commented Mar 22, 2012 at 13:02
2
$\begingroup$

You can supply a third argument to Map and MapThread to indicate at which level in the array the function should be applied. For example for you nested Map example you could do something like

Map[(# + 1 + Min[#]) &, list1, {3}]

For the MapThread example you could do something similar, e.g.

MapThread[(# + 1/Min[#2]) &, {list1, list2}, 3]

would apply the function in the first argument to elements in list1 and list2 at level 3.

$\endgroup$

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.