How to calculate the Cartesian product of two vectors?

44 Views Asked by At

Is there an efficient method to calculate the Cartesian product of two vectors in DolphinDB?

For example, given the vectors ['aa', 'bb'] and ['cc', 'dd'], the desired result is ['aacc', 'aadd', 'bbcc', 'bbdd'] without regard for the order. Your suggestions will be highly appreciated!

2

There are 2 best solutions below

0
Lemonina On
vec1 = ['aa', 'bb'];
vec2 = ['cc', 'dd'];
result = cross(vec1, vec2);

result will contain ['aacc', 'aadd', 'bbcc', 'bbdd']

0
M H On

These two pages refer to the same thing (cross joins):

https://dolphindb.com/help/SQLStatements/TableJoiners/crossjoin.html?highlight=cartesian

https://dolphindb.com/help/FunctionsandCommands/FunctionReferences/c/cj.html?highlight=cartesian

Example using cj()

a = table(2010 2011 2012 as year)
b = table(`IBM`C`AAPL as Ticker);
cj(a,b);

Result

year  Ticker
2010  IBM
2010  C
2010  AAPL  
2011  IBM
2011  C
2011  AAPL  
2012  IBM
2012  C
2012  AAPL  

Try this

a = table(aa bb as x)
b = table(cc dd as y);
cj(a,b);