Generating an "all-combinations" matrix for a given vector
Afficher commentaires plus anciens
Hi,
Given a column vector with integer values, say x = [2;3;4], I would like to create a 3*24 matrix of the following form

Each column in A is created by selecting 1st, 2nd and 3rd elements of the corresponding column from sets {1,2}, {1,2,3} and {1,2,3,4}, respectively. Thus, 2*3*4=24 different columns can be generated using this approach to form matrix A.
I am aware that the number of columns in matrix A grows very fast with the size of vector x and numeric values of its elements. But I won't be working with examples of matrix A larger than 500 cloumns.
Thanks for your help,
-burak
Réponse acceptée
Plus de réponses (2)
Andrei Bobrov
le 7 Nov 2016
Modifié(e) : Andrei Bobrov
le 8 Nov 2016
A = fullfact(x(end:-1:1));
A = flip(A,2)';
other way :)
x = [2 3 4]';
k = arrayfun(@(ii)1:ii,x(end:-1:1),'un',0);
[a,b,c] = ndgrid(k{:});
A = [c(:),b(:),a(:)]';
1 commentaire
Burak
le 8 Nov 2016
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!