6

Given two matrices m1 and m2, e.g.:

m1 = {{a1, b1}, {c1, d1}}
m2 = {{a2, b2}, {c2, d2}}

How can one obtain the following?

{{f[a1, a2], f[b1, b2]}, {f[c1, c2], f[d1 ,d2]}}

I found this solution

MapThread[f, {m1, m2}, 2]

Is there a simpler way?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
dnet
  • 958
  • 4
  • 12
  • 1
    is defining f as Listable an option? – Pinguin Dirk Aug 27 '13 at 18:40
  • Yes. It is an option. – dnet Aug 27 '13 at 18:44
  • 1
    This is an exact copy of a question I asked here http://mathematica.stackexchange.com/questions/29856/correct-way-to-map-a-function-on-entries-down-the-pages-in-a-3d-matrix where the example I had there had 3 matrices, and you have 2 matrices. So you can use the same exact answers there (there are total of 8 ways shown all together there) – Nasser Aug 27 '13 at 19:00
  • @Nasser you're correct. Although I think Pinguin's Listable method is simpler than the solutions proposed on your question. – rcollyer Aug 27 '13 at 19:07
  • @Nasser Thank you for pointing me to your question! – dnet Aug 27 '13 at 19:12
  • @rcollyer, yes, I said so in the comment below. The question is the same though, but the answer given here is much simpler. – Nasser Aug 27 '13 at 19:13
  • @Nasser never noticed the comment. :P – rcollyer Aug 27 '13 at 19:15
  • Related/Possible duplicate: http://mathematica.stackexchange.com/q/17400 – rm -rf Aug 27 '13 at 22:19

2 Answers2

11

Based on the comments, Listable is a possible way for you. Thus, you could:

SetAttributes[f,Listable]

and then simply:

f[m1,m2]

to obtain:

{{f[a1, a2], f[b1, b2]}, {f[c1, c2], f[d1, d2]}}

EDIT

To apply this on a built-in (non-Listable function) like List on could do, as noted by @rcollyer below:

f[m1,m2]/.f->List

(please also note his comment with regard to Block!)

Pure function approach

I also propose the following idea, which saves us from the trouble of making the keyfunction Listable:

Function[{x, y}, anyFunction[x, y], Listable][m1, m2]

The idea is to use a pure function that is Listable, thus we do not have to modify anyFunction. This works with List (instead of anyFunction) etc. as well.

Pinguin Dirk
  • 6,519
  • 1
  • 26
  • 36
  • Very nice. Thank you. – dnet Aug 27 '13 at 18:46
  • What if f is just List? Is there a special solution for that case? – dnet Aug 27 '13 at 18:51
  • 2
    @dnet f[m1, m2] /. f -> List. I would wrap the whole thing in Block, though: e.g. Block[{f}, SetAttributes[f,Listable]; f[m1, m2] /. f -> List], as this eliminates unintentional interactions with the rest of your code. – rcollyer Aug 27 '13 at 19:02
  • You should add this nice answer to my question http://mathematica.stackexchange.com/questions/29856/correct-way-to-map-a-function-on-entries-down-the-pages-in-a-3d-matrix also. No one thought about it this way. Would have accepted this one if I saw it there :) – Nasser Aug 27 '13 at 19:06
  • @Pinguin I would have given you +1, but I forgot. The update, though seals it. :) – rcollyer Aug 27 '13 at 19:16
  • rcollyer: Thanks! @Nasser: Ok, I'll add it there and let the moderators decide what to close as duplicate (or leave it all open) (will add a link to this question there as well) – Pinguin Dirk Aug 27 '13 at 19:23
  • @PinguinDirk don't add it. A close and merge can be done. – rcollyer Aug 27 '13 at 19:26
  • I think it is good to keep this question separate. My question presented each 2D matrix as being already a page of one 3D matrix variable. This question started by giving each 2D matrix as a separate variable. I think it is this slight way of looking at it, made it hard to notice that one can use Listable. But your answer will work for me, if one uses each page as separate matrix and also assume f is Listable (I also did not assume this in my question, which is also a reason may be no one thought of your answer as possible way of doing it). – Nasser Aug 27 '13 at 19:28
  • uhmmm. Now I am definitely confused :). I guess I won't do anything till a mod decides how to proceed... Don't wanna double-post the same idea. Thanks for the feedback! – Pinguin Dirk Aug 27 '13 at 19:33
  • @Nasser, for your question it would just be case of applying the listable function f@@m – Simon Woods Aug 27 '13 at 20:41
  • @Nasser, I think it's fairer if I leave it for Pinguin – Simon Woods Aug 27 '13 at 21:01
  • @SimonWoods: Thanks for the comment, and it seems mod's don't merge/close/whatever the questions, so I just added this idea to Nasser's question. It's getting harder these days to be aware of all the related questions... – Pinguin Dirk Aug 28 '13 at 06:24
2

Perhaps not in spirit and purely for this configuration (i.e. not general enough)

f@@@ # & /@ {m1, m2}

ubpdqn
  • 60,617
  • 3
  • 59
  • 148