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?
답변 (2개)
Jan
2011년 2월 17일
1 개 추천
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
2011년 2월 16일
0 개 추천
This file should do what you want.
댓글 수: 1
Jos (10584)
2011년 2월 17일
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{:})
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!