Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose you have a matrix (mxn), simplified for illustration

m= {{a, b, c}, {d, e, f}} 

And a set of combinations (simplified for illustration purposes but can contain more combinations...)

combinations = {{1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}, {-1, 1, 1}, {-1, 1, -1}, {-1, -1, 1}, {-1, -1, -1}}

What is the best way to derive a matrix that holds all the combinations of the matrix m? As an illustration:

m1 = {{a, b, c}, {a, b, -c}, {a, -b, c} … … …  {-d,-e,-f} }
share|improve this question

4 Answers 4

up vote 3 down vote accepted

Edit

Here is a pretty fast way:

Transpose[
 Transpose[m] Transpose[ConstantArray[combinations, Length[m]], {2, 3, 1}],
 {3, 1, 2}]

(* {{{a, b, c}, {a, b, -c}, {a, -b, c}, {a, -b, -c},
     {-a, b, c}, {-a, b, -c}, {-a, -b, c}, {-a, -b, -c}},
    {{d, e, f}, {d, e, -f}, {d, -e, f}, {d, -e, -f},
     {-d, e, f}, {-d, e, -f}, {-d, -e, f}, {-d, -e, -f}} *)

Like my others, it needs to be flattened 1 level (Flatten[%, 1]) to get it in the exact form requested.

Comparison:

m = RandomReal[9, {150000, 3}];

Flatten[Transpose[
      Transpose[m] Transpose[ConstantArray[combinations, Length[m]], {2, 3, 1}],
      {3, 1, 2}], 1] // Timing // First]; (* Michael E2*)
Join @@ Tuples /@ Outer[Times, m, {1, -1}] // Timing // First (*Mr.Wizard*)
Times @@@ Tuples[{m, Tuples[{1, -1}, 3]}] // Timing // First (*belisarius*)

(* 0.107418
   0.671081
   1.934405 *)

Original answer

A few variations on @belisarius' way, in the sense that they're all about lining rows up for Times. But since he's already used Tuples, these all need some flattening (or for variety, via Sequence in the last two).

Flatten[Outer[Times, m, combinations, 1], 1]

Flatten[(Function[x, x #] /@ combinations) & /@ m, 1]

Block[{fn}, fn[row__] := Sequence @@ (# {row} & /@ combinations);
  fn @@@ m ]

m /. x_?VectorQ :> Sequence @@ (x # & /@ combinations)
share|improve this answer

You can use to find all combinations for 1 & -1.

t = Tuples[{1, -1}, 3]

{{1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}, {-1, 1, 1}, {-1, 1, -1}, {-1, -1, 1}, {-1, -1, -1}}

than just find product,

{t.m[[1]], t.m[[2]]}

{{a + b + c, a + b - c, a - b + c, a - b - c, -a + b + c, -a + b - c, -a - b + c, -a - b - c}, {d + e + f, d + e - f, d - e + f, d - e - f, -d + e + f, -d + e - f, -d - e + f, -d - e - f}}

or Simply

Partition[Flatten[t.# & /@ m], 1]

{{a + b + c}, {a + b - c}, {a - b + c}, {a - b - c}, {-a + b + c}, {-a + b - c}, {-a - b + c}, {-a - b - c}, {d + e + f}, {d + e - f}, {d - e + f}, {d - e - f}, {-d + e + f}, {-d + e - f}, {-d - e + f}, {-d - e - f}}

Edit:Considering above output as t1.

 Flatten[Table[t1[[i]] /. Plus -> List, {i, Length[t1]}], 1]

{{a, b, c}, {a, b, -c}, {a, -b, c}, {a, -b, -c}, {-a, b, c}, {-a, b, -c}, {-a, -b, c}, {-a, -b, -c}, {d, e, f}, {d, e, -f}, {d, -e, f}, {d, -e, -f}, {-d, e, f}, {-d, e, -f}, {-d, -e, f}, {-d, -e, -f}}

share|improve this answer
    
that doesn’t work… note that I am interested in a final matrix of the form: {{a,b,c},{a,b,-c}} not {{a+b+c},{a+b-c}} as you have it... –  Pam Aug 18 '13 at 15:26

Perhaps easier:

m = {{a, b, c}, {d, e, f}};
combinations = Tuples[{1, -1}, 3];
Times @@@ Tuples[{m, combinations}]

{a,b,c} {a,b,-c} {a,-b,c} {a,-b,-c} {-a,b,c} {-a,b,-c} {-a,-b,c} {-a,-b,-c} {d,e,f} {d,e,-f} {d,-e,f} {d,-e,-f} {-d,e,f} {-d,e,-f} {-d,-e,f} {-d,-e,-f}

share|improve this answer
    
Brilliant. Thank you. –  Pam Aug 18 '13 at 16:41

Here is code that is faster than your presently Accepted solution:

m = {{a, b, c}, {d, e, f}};

Tuples /@ Outer[Times, m, {1, -1}]

{{{a, b, c}, {a, b, -c}, {a, -b, c}, {a, -b, -c}, {-a, b, c}, {-a, b, -c}, {-a, -b, c}, {-a, -b, -c}},
{{d, e, f}, {d, e, -f}, {d, -e, f}, {d, -e, -f}, {-d, e, f}, {-d, e, -f}, {-d, -e, f}, {-d, -e, -f}}}

You can use Join @@ if you want all triplets in a single list. Timings:

m = RandomReal[9, {150000, 3}];

Join @@ Tuples /@ Outer[Times, m, {1, -1}] // Timing // First (* Mr.Wizard *)
Times @@@ Tuples[{m, Tuples[{1, -1}, 3]}]  // Timing // First (* belisarius *)

0.437

1.014

share|improve this answer

Your Answer

 
discard

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.