Operation acting on arbitrary number of matrices, element-wise












2












$begingroup$


I have a certain number of $N times M$ matrices:



$$ M_1 = begin{pmatrix} a_{11} & a_{12} & ... & a_{1M} \
a_{21} & & & \
vdots & & ddots & \
a_{N1} & & & a_{NM}
end{pmatrix}
$$



$$ M_2 = begin{pmatrix} b_{11} & b_{12} & ... & b_{1M} \
b_{21} & & & \
vdots & & ddots & \
b_{N1} & & & b_{NM}
end{pmatrix}
$$



and I want to create a new matrix, applying a certain operation $f$ element by element, obtaining something like



$$ M = begin{pmatrix} f(a_{11},b_{11},...) & f(a_{12},b_{12},...) & ... & f(a_{1M},b_{1M},...) \
f(a_{21},b_{21},...) & & & \
vdots & & ddots & \
f(a_{N1},b_{N1},...) & & & f(a_{NM},b_{NM},...)
end{pmatrix}
$$



where the $f$ takes as many arguments as the number of matrices.



As of now I am implementing this using a Table,



With[{dims = Dimensions[dataA]}, Table[f[dataA[[x, y]], dataB[[x, y]], dataC[[x, y]]], {x, 1, dims[[1]]}, {y, 1, dims[[2]]}]]]


I was wondering if there's some more idiomatic Mathematica way of doing this.










share|improve this question









$endgroup$

















    2












    $begingroup$


    I have a certain number of $N times M$ matrices:



    $$ M_1 = begin{pmatrix} a_{11} & a_{12} & ... & a_{1M} \
    a_{21} & & & \
    vdots & & ddots & \
    a_{N1} & & & a_{NM}
    end{pmatrix}
    $$



    $$ M_2 = begin{pmatrix} b_{11} & b_{12} & ... & b_{1M} \
    b_{21} & & & \
    vdots & & ddots & \
    b_{N1} & & & b_{NM}
    end{pmatrix}
    $$



    and I want to create a new matrix, applying a certain operation $f$ element by element, obtaining something like



    $$ M = begin{pmatrix} f(a_{11},b_{11},...) & f(a_{12},b_{12},...) & ... & f(a_{1M},b_{1M},...) \
    f(a_{21},b_{21},...) & & & \
    vdots & & ddots & \
    f(a_{N1},b_{N1},...) & & & f(a_{NM},b_{NM},...)
    end{pmatrix}
    $$



    where the $f$ takes as many arguments as the number of matrices.



    As of now I am implementing this using a Table,



    With[{dims = Dimensions[dataA]}, Table[f[dataA[[x, y]], dataB[[x, y]], dataC[[x, y]]], {x, 1, dims[[1]]}, {y, 1, dims[[2]]}]]]


    I was wondering if there's some more idiomatic Mathematica way of doing this.










    share|improve this question









    $endgroup$















      2












      2








      2





      $begingroup$


      I have a certain number of $N times M$ matrices:



      $$ M_1 = begin{pmatrix} a_{11} & a_{12} & ... & a_{1M} \
      a_{21} & & & \
      vdots & & ddots & \
      a_{N1} & & & a_{NM}
      end{pmatrix}
      $$



      $$ M_2 = begin{pmatrix} b_{11} & b_{12} & ... & b_{1M} \
      b_{21} & & & \
      vdots & & ddots & \
      b_{N1} & & & b_{NM}
      end{pmatrix}
      $$



      and I want to create a new matrix, applying a certain operation $f$ element by element, obtaining something like



      $$ M = begin{pmatrix} f(a_{11},b_{11},...) & f(a_{12},b_{12},...) & ... & f(a_{1M},b_{1M},...) \
      f(a_{21},b_{21},...) & & & \
      vdots & & ddots & \
      f(a_{N1},b_{N1},...) & & & f(a_{NM},b_{NM},...)
      end{pmatrix}
      $$



      where the $f$ takes as many arguments as the number of matrices.



      As of now I am implementing this using a Table,



      With[{dims = Dimensions[dataA]}, Table[f[dataA[[x, y]], dataB[[x, y]], dataC[[x, y]]], {x, 1, dims[[1]]}, {y, 1, dims[[2]]}]]]


      I was wondering if there's some more idiomatic Mathematica way of doing this.










      share|improve this question









      $endgroup$




      I have a certain number of $N times M$ matrices:



      $$ M_1 = begin{pmatrix} a_{11} & a_{12} & ... & a_{1M} \
      a_{21} & & & \
      vdots & & ddots & \
      a_{N1} & & & a_{NM}
      end{pmatrix}
      $$



      $$ M_2 = begin{pmatrix} b_{11} & b_{12} & ... & b_{1M} \
      b_{21} & & & \
      vdots & & ddots & \
      b_{N1} & & & b_{NM}
      end{pmatrix}
      $$



      and I want to create a new matrix, applying a certain operation $f$ element by element, obtaining something like



      $$ M = begin{pmatrix} f(a_{11},b_{11},...) & f(a_{12},b_{12},...) & ... & f(a_{1M},b_{1M},...) \
      f(a_{21},b_{21},...) & & & \
      vdots & & ddots & \
      f(a_{N1},b_{N1},...) & & & f(a_{NM},b_{NM},...)
      end{pmatrix}
      $$



      where the $f$ takes as many arguments as the number of matrices.



      As of now I am implementing this using a Table,



      With[{dims = Dimensions[dataA]}, Table[f[dataA[[x, y]], dataB[[x, y]], dataC[[x, y]]], {x, 1, dims[[1]]}, {y, 1, dims[[2]]}]]]


      I was wondering if there's some more idiomatic Mathematica way of doing this.







      matrix






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      zakkzakk

      458514




      458514






















          2 Answers
          2






          active

          oldest

          votes


















          4












          $begingroup$

          MapThread is designed to do exactly this. Example:



          A = {{a11, a12}, {a21, a22}}; 
          B = {{b11, b12}, {b21, b22}};
          MapThread[f, {A, B}, 2]


          Gives



          {{f[a11, b11], f[a12, b12]}, {f[a21, b21], f[a22, b22]}}


          The 2 is because you want to apply to elements of lists of lists. The arguments to "f" are 2 levels deep.






          share|improve this answer








          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            Exactly what I was looking for! Thanks!
            $endgroup$
            – zakk
            17 mins ago



















          3












          $begingroup$

          m1 = Array[a, {3, 3}];
          m2 = Array[b, {3, 3}];
          SetAttributes[foo, Listable]
          foo[m1, m2] // MatrixForm // TeXForm



          $left(
          begin{array}{ccc}
          text{foo}(a(1,1),b(1,1)) & text{foo}(a(1,2),b(1,2)) & text{foo}(a(1,3),b(1,3)) \
          text{foo}(a(2,1),b(2,1)) & text{foo}(a(2,2),b(2,2)) & text{foo}(a(2,3),b(2,3)) \
          text{foo}(a(3,1),b(3,1)) & text{foo}(a(3,2),b(3,2)) & text{foo}(a(3,3),b(3,3)) \
          end{array}
          right)$







          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you very much!
            $endgroup$
            – zakk
            17 mins ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "387"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190007%2foperation-acting-on-arbitrary-number-of-matrices-element-wise%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4












          $begingroup$

          MapThread is designed to do exactly this. Example:



          A = {{a11, a12}, {a21, a22}}; 
          B = {{b11, b12}, {b21, b22}};
          MapThread[f, {A, B}, 2]


          Gives



          {{f[a11, b11], f[a12, b12]}, {f[a21, b21], f[a22, b22]}}


          The 2 is because you want to apply to elements of lists of lists. The arguments to "f" are 2 levels deep.






          share|improve this answer








          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            Exactly what I was looking for! Thanks!
            $endgroup$
            – zakk
            17 mins ago
















          4












          $begingroup$

          MapThread is designed to do exactly this. Example:



          A = {{a11, a12}, {a21, a22}}; 
          B = {{b11, b12}, {b21, b22}};
          MapThread[f, {A, B}, 2]


          Gives



          {{f[a11, b11], f[a12, b12]}, {f[a21, b21], f[a22, b22]}}


          The 2 is because you want to apply to elements of lists of lists. The arguments to "f" are 2 levels deep.






          share|improve this answer








          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            Exactly what I was looking for! Thanks!
            $endgroup$
            – zakk
            17 mins ago














          4












          4








          4





          $begingroup$

          MapThread is designed to do exactly this. Example:



          A = {{a11, a12}, {a21, a22}}; 
          B = {{b11, b12}, {b21, b22}};
          MapThread[f, {A, B}, 2]


          Gives



          {{f[a11, b11], f[a12, b12]}, {f[a21, b21], f[a22, b22]}}


          The 2 is because you want to apply to elements of lists of lists. The arguments to "f" are 2 levels deep.






          share|improve this answer








          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$



          MapThread is designed to do exactly this. Example:



          A = {{a11, a12}, {a21, a22}}; 
          B = {{b11, b12}, {b21, b22}};
          MapThread[f, {A, B}, 2]


          Gives



          {{f[a11, b11], f[a12, b12]}, {f[a21, b21], f[a22, b22]}}


          The 2 is because you want to apply to elements of lists of lists. The arguments to "f" are 2 levels deep.







          share|improve this answer








          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 1 hour ago









          or1426or1426

          1562




          1562




          New contributor




          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          or1426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • $begingroup$
            Exactly what I was looking for! Thanks!
            $endgroup$
            – zakk
            17 mins ago


















          • $begingroup$
            Exactly what I was looking for! Thanks!
            $endgroup$
            – zakk
            17 mins ago
















          $begingroup$
          Exactly what I was looking for! Thanks!
          $endgroup$
          – zakk
          17 mins ago




          $begingroup$
          Exactly what I was looking for! Thanks!
          $endgroup$
          – zakk
          17 mins ago











          3












          $begingroup$

          m1 = Array[a, {3, 3}];
          m2 = Array[b, {3, 3}];
          SetAttributes[foo, Listable]
          foo[m1, m2] // MatrixForm // TeXForm



          $left(
          begin{array}{ccc}
          text{foo}(a(1,1),b(1,1)) & text{foo}(a(1,2),b(1,2)) & text{foo}(a(1,3),b(1,3)) \
          text{foo}(a(2,1),b(2,1)) & text{foo}(a(2,2),b(2,2)) & text{foo}(a(2,3),b(2,3)) \
          text{foo}(a(3,1),b(3,1)) & text{foo}(a(3,2),b(3,2)) & text{foo}(a(3,3),b(3,3)) \
          end{array}
          right)$







          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you very much!
            $endgroup$
            – zakk
            17 mins ago
















          3












          $begingroup$

          m1 = Array[a, {3, 3}];
          m2 = Array[b, {3, 3}];
          SetAttributes[foo, Listable]
          foo[m1, m2] // MatrixForm // TeXForm



          $left(
          begin{array}{ccc}
          text{foo}(a(1,1),b(1,1)) & text{foo}(a(1,2),b(1,2)) & text{foo}(a(1,3),b(1,3)) \
          text{foo}(a(2,1),b(2,1)) & text{foo}(a(2,2),b(2,2)) & text{foo}(a(2,3),b(2,3)) \
          text{foo}(a(3,1),b(3,1)) & text{foo}(a(3,2),b(3,2)) & text{foo}(a(3,3),b(3,3)) \
          end{array}
          right)$







          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you very much!
            $endgroup$
            – zakk
            17 mins ago














          3












          3








          3





          $begingroup$

          m1 = Array[a, {3, 3}];
          m2 = Array[b, {3, 3}];
          SetAttributes[foo, Listable]
          foo[m1, m2] // MatrixForm // TeXForm



          $left(
          begin{array}{ccc}
          text{foo}(a(1,1),b(1,1)) & text{foo}(a(1,2),b(1,2)) & text{foo}(a(1,3),b(1,3)) \
          text{foo}(a(2,1),b(2,1)) & text{foo}(a(2,2),b(2,2)) & text{foo}(a(2,3),b(2,3)) \
          text{foo}(a(3,1),b(3,1)) & text{foo}(a(3,2),b(3,2)) & text{foo}(a(3,3),b(3,3)) \
          end{array}
          right)$







          share|improve this answer









          $endgroup$



          m1 = Array[a, {3, 3}];
          m2 = Array[b, {3, 3}];
          SetAttributes[foo, Listable]
          foo[m1, m2] // MatrixForm // TeXForm



          $left(
          begin{array}{ccc}
          text{foo}(a(1,1),b(1,1)) & text{foo}(a(1,2),b(1,2)) & text{foo}(a(1,3),b(1,3)) \
          text{foo}(a(2,1),b(2,1)) & text{foo}(a(2,2),b(2,2)) & text{foo}(a(2,3),b(2,3)) \
          text{foo}(a(3,1),b(3,1)) & text{foo}(a(3,2),b(3,2)) & text{foo}(a(3,3),b(3,3)) \
          end{array}
          right)$








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          kglrkglr

          180k9200413




          180k9200413












          • $begingroup$
            Thank you very much!
            $endgroup$
            – zakk
            17 mins ago


















          • $begingroup$
            Thank you very much!
            $endgroup$
            – zakk
            17 mins ago
















          $begingroup$
          Thank you very much!
          $endgroup$
          – zakk
          17 mins ago




          $begingroup$
          Thank you very much!
          $endgroup$
          – zakk
          17 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematica Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190007%2foperation-acting-on-arbitrary-number-of-matrices-element-wise%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'