Operation acting on arbitrary number of matrices, element-wise
$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.
matrix
$endgroup$
add a comment |
$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.
matrix
$endgroup$
add a comment |
$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.
matrix
$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
matrix
asked Jan 22 at 16:14
zakkzakk
463514
463514
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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.
$endgroup$
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
add a comment |
$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)$
$endgroup$
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
$endgroup$
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
add a comment |
$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.
$endgroup$
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
add a comment |
$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.
$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.
answered Jan 22 at 16:23
or1426or1426
1562
1562
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
add a comment |
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
$begingroup$
Exactly what I was looking for! Thanks!
$endgroup$
– zakk
Jan 22 at 17:10
add a comment |
$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)$
$endgroup$
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
add a comment |
$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)$
$endgroup$
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
add a comment |
$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)$
$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)$
answered Jan 22 at 16:25
kglrkglr
183k10202417
183k10202417
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
add a comment |
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
$begingroup$
Thank you very much!
$endgroup$
– zakk
Jan 22 at 17:11
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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