How to generate all pairs from two vectors in MATLAB using vectorised code?

Try

[p,q] = meshgrid(vec1, vec2);
pairs = [p(:) q(:)];

See the MESHGRID documentation. Although this is not exactly what that function is for, but if you squint at it funny, what you are asking for is exactly what it does.

Leave a Comment