cartesian-product

Generate a matrix containing all combinations of elements taken from n vectors

让人想犯罪 __ 提交于 2019-11-25 22:56:20
问题 This question pops up quite often in one form or another (see for example here or here). So I thought I\'d present it in a general form, and provide an answer which might serve for future reference. Given an arbitrary number n of vectors of possibly different sizes, generate an n -column matrix whose rows describe all combinations of elements taken from those vectors (Cartesian product) . For example, vectors = { [1 2], [3 6 9], [10 20] } should give combs = [ 1 3 10 1 3 20 1 6 10 1 6 20 1 9

Generating all Possible Combinations

烂漫一生 提交于 2019-11-25 22:24:41
问题 Given 2 arrays Array1 = {a,b,c...n} and Array2 = {10,20,15....x} how can I generate all possible combination as Strings a(i) b(j) c(k) n(p) where 1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x Such as: a1 b1 c1 .... n1 a1 b1 c1..... n2 ...... ...... a10 b20 c15 nx (last combination) So in all total number of combination = product of elements of array2 = (10 X 20 X 15 X ..X x) Similar to a Cartesian product, in which the second array defines the upper limit for each element in

Cartesian product of x and y array points into single array of 2D points

删除回忆录丶 提交于 2019-11-25 22:19:35
问题 I have two numpy arrays that define the x and y axes of a grid. For example: x = numpy.array([1,2,3]) y = numpy.array([4,5]) I\'d like to generate the Cartesian product of these arrays to generate: array([[1,4],[2,4],[3,4],[1,5],[2,5],[3,5]]) In a way that\'s not terribly inefficient since I need to do this many times in a loop. I\'m assuming that converting them to a Python list and using itertools.product and back to a numpy array is not the most efficient form. 回答1: >>> numpy.transpose(

Is there a good LINQ way to do a cartesian product?

六眼飞鱼酱① 提交于 2019-11-25 19:36:24
I have a class structure like so: Person Dogs (dog 1, dog 2, etc) Puppies (puppy A, puppy B, etc) There is one person. He has 1..n dogs. Each dog has 1..n puppies. I want a list of all the possible combination of puppies, taking 1 puppy from each dog. Eg: dog 1 puppy A, dog 2 puppy A dog 1 puppy A, dog 2 puppy B dog 1 puppy B, dog 2 puppy A dog 1 puppy B, dog 2 puppy B If it was in sql tables, i'd do something like the following to 'multiply' the tables: select * from puppies a, puppies b where a.parent='dog1' and b.parent='dog2' Is there some linq-ish way to do this kinda thing??? Thanks so