4
$\begingroup$

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} }
$\endgroup$

4 Answers 4

4
$\begingroup$

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)
$\endgroup$
4
$\begingroup$

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}}

$\endgroup$
1
  • $\begingroup$ 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... $\endgroup$
    – Pam
    Aug 18, 2013 at 15:26
4
$\begingroup$

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}

$\endgroup$
1
  • $\begingroup$ Brilliant. Thank you. $\endgroup$
    – Pam
    Aug 18, 2013 at 16:41
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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