Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have a set of 15 $4\times 4$ matrices which I call $X_i, (i=1,2..15)$ and a set of 6 $4\times 4$ matrices which I call $y_j, (j=1,2...6)$. Now I have to calculate $(X_i y_j)-(y_j X_i^*)$ for different values of $i$ and $j$, where $*$ stands for Complex conjugate. Is there a simple command (with perhaps loops) in mathematica using which I can calculate all the 90 results instead of multiplying these matrices individually 90 times?

share|improve this question
Have a look at Outer. – b.gatessucks Dec 3 '12 at 8:29
Try Outer[#1.#2 - #2.Conjugate[#1] &, X, y] – E.O. Dec 3 '12 at 9:08

Define some matrices.

Xs = RandomInteger[{1, 4}, {2, 4, 4}];
ys = RandomInteger[{1, 4}, {3, 4, 4}];

Use Outer applied to the indices to produce your derived matrices:

Outer[#1.#2 - #2.Conjugate@#1 &, Xs, ys]
, 1]

Flatten provides your result matrices in a single level list.

Or to follow your naming convention of variable names with subscripts:

Define the number of Xs and ys:

numXs = 2;
numys = 3;

  Subscript[X, #1].Subscript[y, #2] - 
    Subscript[y, #2].Conjugate@Subscript[X, #1] &, Range@numXs, 
  Range@numys], 1]
share|improve this answer

X is made of sixteen 4by4 matrix. X[[i]]] is your Xi.

X = Table[RandomReal[1, {4, 4}], {i, 1, 16}];

Y is made of six 4by4 matrix. Y[[j]]] is your Yi.

Y = Table[RandomReal[1, {4, 4}], {i, 1, 6}]; 

I define a function for the multiplication you want:

fun[i_, j_] := X[[i]].Y[[j]] - Y[[j]].X[[i]]\[Conjugate]

Now I make a table out of this function. Each i and j gives one of the 16*4 multiplications.

result = Table[fun[i, j], {i, 1, 16}, {j, 1, 4}];
result[[13, 2]] // Dimensions
{4, 4}

Each element of result as you see is a 4by4 matrix.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.