Message Boards Message Boards

0
|
1452 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Applying Map to two disconnected levels

Posted 11 months ago

I am want to apply the Map function to a nested list, via: Map[f, {{a1, b1}, {a2, b2}, {a3, b3}}, {0, 2}] and get the result: f[{f[{f[a1], f[b1]}], f[{f[a2], f[b2]}], f[{f[a3], f[b3]}]}]

However, I want the function f to only be applied at levels 0 and level 2. Map is applying f on levels 0 to 2. How is this accomplished? The result l want is: f[{ {f[a1], f[b1]}], f[{f[a2], f[b2]}], f[{f[a3], f[b3]} }]

How can this be done? Given the generality of the software, I assumed this was possible. Thanks.

3 Replies
Posted 11 months ago

Using Apply for level 0:

mat = {{a1, b1}, {a2, b2}, {a3, b3}};
f @@ Map[f, mat, {2}]

(* f[{f[a1],f[b1]},{f[a2],f[b2]},{f[a3],f[b3]}] *)
POSTED BY: Hans Milton

It seems that you cannot do it directly with Map, but there are workarounds:

mat = {{a1, b1}, {a2, b2}, {a3, b3}};
f@Map[f, mat, {2}]
Fold[Map[f, #1, {#2}] &, mat, {2, 0}]
POSTED BY: Gianluca Gorni
Posted 11 months ago

Your expected result is syntactically invalid, and so I'm actually not sure how you want to apply f. It seems like some combination of Map-ing and Applying. Also, I see f at three levels in your expected result, but you say you only want to apply it at two levels. Please correct your expected output or clarify your question.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract