cartesian products?

daren.r's Avatar

daren.r

25 Aug, 2020 08:04 PM

coming from a SQL background, im not sure what it's called.. is there an effective way to do a cartesian product of two lists?

example:

(x,y,z) , (1,2,3) becomes...

x1, x2, x3
y1, y2, y3
z1, z2, z3

i can do this manually now with a counter / range / shift .. but im not sure if theres something else i might be missing

  1. Support Staff 1 Posted by john on 26 Aug, 2020 12:36 AM

    john's Avatar

    Hi Daren,

    I made a simple Cartesian Product node for you (technically a subnetwork). See zip file and two screenshots (attached).

    If you look inside the cart_prod node you will see that it's absurdly simple: all I do is concatenate the two lists (with an optional delimiter).

    But there is a subtle trick needed to make this work. If you select the node and open the MetaData dialog, then select the set 1 and set 2 ports, you will see that the range for set 1 is list while the range for set 2 is value.

    This means that the node sucks in the entire set 1 list and then fires once for each item in set 2. Each time it fires it produces concatenations (ordered pairs) of every item from set 1 with one item from set 2. The end result is a single list with all combinations.

    This is a wonderful example of the way subnetworks work. Their main purpose is not so much to encapsulate nodes, but rather to choreograph the flow of data. This takes a little getting used to, but is very powerful.

    I provide two examples. You can feed the node strings or numbers, but both lists will be treated as strings. The first case shows the Cartesian Product of {x,y,z} with {1,2,3,4} with no delimiter. The second case joins two different sets of 10 random numbers and uses a comma delimiter. Although the output is a list of strings, you can treat them a points (comma-separated pairs of values) and use that to plot an arrangement of 100 dots.

    Thanks for an interesting question. Keep them coming!

    John

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

18 Sep, 2020 09:16 PM
26 Aug, 2020 12:36 AM
21 Aug, 2020 09:58 PM
20 Aug, 2020 08:16 PM
20 Aug, 2020 07:56 PM

 

16 Aug, 2020 06:40 PM
01 Aug, 2020 11:34 PM
28 Jul, 2020 11:28 AM
27 Jul, 2020 10:47 AM
27 Jul, 2020 08:00 AM
27 Jul, 2020 06:46 AM