1

In numpy there is an efficient solution for generating the cartesian product of two vectors using meshgrid.

Is there a ready-made solution for getting the combinations of all elements in a single vector as per {x_i, x_j : 1 <= i <= j <= N} where N is the length of the vector?

Illustration: Let us asume:

input = [1, 2, 3, 4]

Output should ideally be two vectors that together form the combination of all elements of the input:

output1 = [1, 1, 1, 2, 2, 3]
output2 = [2, 3, 4, 3, 4, 4]

I know how to implement this with python loops but I was looking for a faster solution.

Also: Does this form of combination have a name?

ARF
  • 7,420
  • 8
  • 45
  • 72
  • Maybe answers to https://stackoverflow.com/questions/4709510/itertools-product-speed-up is what you're looking for. – gstukelj Sep 16 '19 at 14:03
  • @GasperStukelj For a normal cartesian product yes. What I am looking for is a "semi-cartesian product" though. If a cartesian product can be thought of as a matrix, my problem would be like a triangular matrix. – ARF Sep 16 '19 at 14:12
  • @Divakar Thanks for link. I was already considering using numpy. Very nice to have a ready-made solution! – ARF Sep 16 '19 at 14:13

0 Answers0