matrix nchoosk for column HELP

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
Jan on 17 Feb 2011
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
Matt Fig on 16 Feb 2011

1 Comment

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{:})

Sign in to comment.

Asked:

on 16 Feb 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!