MATLAB: Create a vector ,of sets of predefined numbers stored in another row vectors matrixvectors Guys,I have two row vectors X=[3:6] and Y=[8:27].I want to create and store all possible combinations of (X,Y) in another vector ,say Z. So my Z will be, Z=[(3,8) (3,9) (3,10)…(3,27) (4,8) (4,9)……(6,26) (6,27)] Best Answer This is called the Cartesian product of the two sets.A fairly straightforward way is to use ndgrid:x = 3:6;y = 8:27;[xx, yy] = ndgrid(x, y);cartprod = [xx(:) yy(:)]This works also for more than two vectors: x = 3:6; y = 8:27; z = 2:2:8; [xx, yy, zz] = ndgrid(x, y, z); cartprod = [xx(:) yy(:) zz(:)] Related SolutionsMATLAB: I need to plot a surface plot from given X and Y coordinate matrixes and z as a constant value. Why do your X and Y have two columns if it means nothing?X = 0:3;Y = 0:3;Z = 0;[XX, YY] = ndgrid(X, Y);surf(XX, YY, repmat(Z, size(XX))); MATLAB: Converting rectangular grids to an array Hi Alex,The concatenationm = [x_mesh(:) y_mesh(:) z_mesh(:)]gives a 27x3 list of all the points, but not in the order you prefer. Doing some permutations on indices works:xx = permute(x_mesh,[3 2 1]);zz = permute(z_mesh,[3 2 1]);m = [xx(:) y_mesh(:) zz(:)] Related QuestionSimplify expression after collect functionGiven matrices XX and YY of sizes 3X3, how can I generate the following matrix: [XX(1,1) YY(1,1); XX(1,2) YY(1,2)…Creating an array of vectorsHow can i solveCombination of X and Y vectors to get all possible positions on a Cartesian plane
Best Answer