Group Abstract Group Abstract

Message Boards Message Boards

0
|
10.4K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Perform element-wise matrix operations (EqualTo)?

POSTED BY: Rebecca A
4 Replies
Posted 10 years ago

You already have a direct and simple solution( flatten first) using basic Mathematica and it works for any depth of nesting. A different way to do that is to use a Listable function (alias for "==") and flatten the result.

eq[x_, y_] = Equal[x, y]; SetAttributes[eq, Listable];
Flatten[eq[amat, bmat]]

{a11 == b11, a12 == b12, a21 == b21, a22 == b22, a31 == b31, 
 a32 == b32, a41 == b41, a42 == b42}
POSTED BY: Douglas Kubler

More elegant would be to use MapThread if you want to do 'deeper' threading, Thread does not have a a level-specification.

amat = {{a11, a12}, {a21, a22}, {a31, a32}, {a41, a42}}; 
bmat = {{b11, b12}, {b21, b22}, {b31, b32}, {b41, b42}};
MapThread[Equal, {amat, bmat}, 2]
Join @@ %

Join @@ can also be replaced by Catenate@ or Flatten@, whatever you like...

POSTED BY: Sander Huisman
In[4]:= Flatten[Thread /@ Thread[amat == bmat]]

Out[4]= {a11 == b11, a12 == b12, a21 == b21, a22 == b22, a31 == b31, 
 a32 == b32, a41 == b41, a42 == b42}
POSTED BY: Frank Kampas
In[1]:= Thread[Flatten[amat] == Flatten[bmat]]

Out[1]= {a11 == b11, a12 == b12, a21 == b21, a22 == b22, a31 == b31, 
 a32 == b32, a41 == b41, a42 == b42}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard