Generate a matrix containing all combinations of elements taken from n vectors
问题 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