matrix nchoosk for column HELP
Afficher commentaires plus anciens
Suppose I have
A=[a1 a2 a3; b1 b2 b3; c1 c2 c3; d1 d2 d3];
I want to be able to generate all the possible vectors [a b c d]'
For example [a1 b1 c1 d1] is one but so is [a2 b1 c1 d2] and [a3 b1 c2 d3]
There are many combinations and depending on the number of rows and columns it will get explosive fast.
But how do I do this? Can it be extended to a matrix with an arbitrary number of rows and columns?
Réponses (2)
Jan
le 17 Fév 2011
1 vote
You can create the indices for each single column by:
To get the linear index related to the matrix, add (0:size(A,1):numel(A)-1) to the created array - you will need BSXFUN for that.
Matt Fig
le 16 Fév 2011
0 votes
This file should do what you want.
1 commentaire
Jos (10584)
le 17 Fév 2011
Indeed, just feed the rows of A to allcomb
R = allcomb(A(1,:), A(2,:), A(3,:), A(4,:))
For a little more flexibility you can create cell array and use comma-separated list expansion:
C = mat2cell(A, ones(1,size(A,1))) ;
R2 = allcomb(C{:})
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!