MATLAB: Generating matrices of the possible combinations

combinationsmatrix

How do I find all the possible matricies (not their number but the matricies themselves) that are formed as a result of taking a combination of a certain number of columns y from a matrix with a total number of columns x ?
example
X = 1 7 9 10 15
4 8 5 9 21
2 14 3 6 16
here X is 3 by 5 ( 5 columns), now suppose I want to obtain the MATRICIES THEMSELVES that are the result of mixing any of the 3 columns of matrix X
In this case 5c3 , i should obtain 10 matricies, how do I get them ?

Best Answer

X = [1,7,9,10,15;4,8,5,9,21;2,14,3,6,16]
P = nchoosek(1:5,3)
N = size(P,1);
C = cell(1,N);
for k = 1:N
C{k} = X(:,P(k,:));
end
And checking the output:
>> numel(C)
ans = 10
>> C{:}
ans =
1 7 9
4 8 5
2 14 3
ans =
1 7 10
4 8 9
2 14 6
ans =
1 7 15
4 8 21
2 14 16
ans =
1 9 10
4 5 9
2 3 6
ans =
1 9 15
4 5 21
2 3 16
ans =
1 10 15
4 9 21
2 6 16
ans =
7 9 10
8 5 9
14 3 6
ans =
7 9 15
8 5 21
14 3 16
ans =
7 10 15
8 9 21
14 6 16
ans =
9 10 15
5 9 21
3 6 16