Question

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?

Was it helpful?

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

OTHER TIPS

I'm in search of the same thing.

I've only came up with the following that are shorter but not prettier:

,/(N0 ,. ])"0 N1

;(N0 ,. ]) &.> N1

or in the form:

;N0&,.&.>N1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top