Search code examples
j

Unique pairs of unequal arrays in J


Suppose two arrays of different sizes:

N0 =: i. 50
N1 =: i. 500

There should be a way to get the unique pairs, just combine the two. The "simplest" I found was:

]$R =: |:,"2 |: (,.N0) ,"1 0/ N1
25000 2

Which is frankly a butt ugly, baseball bat solution. Is there a more elegant way to do this?


Solution

  • The pattern of data you're reaching for is a variation on Catalogue. It's the most famous variation, in fact: Cartesian product.

    On the Vocabulary listing for Catalogue there's also code for Cartesian product. To get the list you want, just ravel and open the result.

    pair=: >@ ,@ { @(,&<)
    
       $ N0 pair N1
    25000 2