I want to compute all combinations of a given matrix.

I came up with

fun[mat_] := 
 Outer[List, Apply[Range, {First@mat[[1]], Last@mat[[1]]}], 
  Apply[Range, {First@mat[[2]], Last@mat[[2]]}]]

for a matrix with two rows. Now I want to generalize this function for a matrix with more than two sublists. But I am facing some difficulties with specifying the arguments in Outer correctly (I don't want to rewrite Apply[Range, {First@mat[[1]], Last@mat[[1]] again and again).

Any help is highly appreciated.


1 Answer 1


I am not entirely sure whether this is what you look for. Both variants should work equally well.

fun2[mat_] := Outer[List, Sequence @@ Range @@@ mat[[All, {1, -1}]], 1];
fun3[mat_] := Outer[List, ##, 1] & @@ Range @@@ mat[[All, {1, -1}]];
  • $\begingroup$ Yes, that is exactly what I am looking for. Somehow didn't get it together with the different Applys. Thank you! $\endgroup$
    – NeverMind
    Commented Dec 18, 2018 at 10:15
  • $\begingroup$ You're welcome. $\endgroup$ Commented Dec 18, 2018 at 10:26

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.