matrix nchoosk for column HELP
Show older comments
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?
Answers (2)
Jan
on 17 Feb 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
on 16 Feb 2011
0 votes
This file should do what you want.
1 Comment
Jos (10584)
on 17 Feb 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{:})
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!