0

I have this array and I want to return unique array combinations. I tried meshgrid but it creates duplicates and inverse array values

>> import numpy as np
>> array = np.array([0,1,2,3])
>> combinations = np.array(np.meshgrid(array, array)).T.reshape(-1,2)
>> print(combinations)

[[0 0] 
 [0 1]
 [0 2]
 [0 3]
 [1 0]
 [1 1]
 [1 2]
 [1 3]
 [2 0]
 [2 1]
 [2 2]
 [2 3]
 [3 0]
 [3 1]
 [3 2]
 [3 3]]

What I want to exclude are the repeating arrays: [0,0] [1,1] [2,2] [3,3] and the inverse arrays when [2,3] is returned exclude [3,2] in the output.

Take a look at this combination calculator, this is the output that I like but how can I create it in NumPy?

1 Answer 1

2

you could use combinations from itertools

import numpy as np
from itertools import combinations

array = np.array([0,1,2,3])
combs = np.array(list(combinations(arr, 2)))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.