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?

Antworten (2)

Jan
Jan am 17 Feb. 2011

1 Stimme

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 am 16 Feb. 2011

0 Stimmen

1 Kommentar

Jos (10584)
Jos (10584) am 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{:})

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 16 Feb. 2011

Community Treasure Hunt

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

Start Hunting!

Translated by