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

  2. 2 Posted by daren.r on 29 Sep, 2020 03:19 AM

    daren.r's Avatar

    Thank you!

  3. daren.r closed this discussion on 23 Feb, 2023 01:58 AM.

Comments are currently closed for this discussion. You can start a new one.

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

13 Jul, 2024 06:05 AM
11 Jul, 2024 03:48 PM
28 Jun, 2024 01:51 PM
27 Jun, 2024 02:56 PM
27 Jun, 2024 01:57 PM

 

26 Jun, 2024 08:17 PM
26 Jun, 2024 11:35 AM
24 Jun, 2024 02:42 AM
24 Jun, 2024 12:46 AM
23 Jun, 2024 08:07 AM
08 Jun, 2024 09:05 AM