What is the equivalent of Matlab / Octave or R 'merge' (or 'expand.grid')?

I am looking for a way for Matlab to do the following:

> merge(2:4,3:7) xy 1 2 3 2 3 3 3 4 3 4 2 4 5 3 4 6 4 4 7 2 5 8 3 5 9 4 5 10 2 6 11 3 6 12 4 6 13 2 7 14 3 7 15 4 7 > expand.grid(2:4,3:7) Var1 Var2 1 2 3 2 3 3 3 4 3 4 2 4 5 3 4 6 4 4 7 2 5 8 3 5 9 4 5 10 2 6 11 3 6 12 4 6 13 2 7 14 3 7 15 4 7 
+7
source share
2 answers

I usually do this with a meshgrid :

 >> [xy] = meshgrid(2:4, 3:7); >> [x(:) y(:)] ans = 2 3 2 4 2 5 2 6 2 7 3 3 3 4 3 5 3 6 3 7 4 3 4 4 4 5 4 6 4 7 
+11
source

Use ndgrid for n variables (2 or more). For example, (4-dimensional space)

 [X,Y,Z,T] = ndgrid(2:4, 3:7, 1:2, 1:10); 
+1
source

All Articles