Math Question: Is it possible to calculate the row position as you loop through a cartesian product

Posted by Kuyenda on Stack Overflow See other posts from Stack Overflow or by Kuyenda
Published on 2010-05-13T01:28:37Z Indexed on 2010/05/13 1:34 UTC
Read the original article Hit count: 215

Filed under:

Is it possible to calculate the row position in the cartesian product of two arrays?

For example if you have one array of two rows and another of three rows it's easy to calculate the size of the cartesian product (Array1.Rows.Count * Array2.Rows.Count = 6), but you can't iterate through each array and just use the product of the respective row positions to calculate the row position in the cartesian product.

Array1.Row * Array2.Row
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6

Is there a formula to obtain the result 1, 2, 3, 4, 5, 6 from Array1.Row and Array2.Row as you iterate through them in the following fashion:

For 1 To Array1.Rows.Count
    For 1 To Array2.Rows.Count
        'some formula here to get cartesian row position'
    Next Array2.Row
Next Array1.Row

Thanks!

© Stack Overflow or respective owner

Related posts about cartesian-product