generating matrices of the possible combinations
5 views (last 30 days)
Show older comments
Abeer Abdelhadi
on 6 Dec 2018
Commented: Abeer Abdelhadi
on 6 Dec 2018
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 ?
Accepted Answer
Stephen23
on 6 Dec 2018
Edited: Stephen23
on 6 Dec 2018
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
More Answers (1)
Luna
on 6 Dec 2018
Try this;
M is a 1x10 cell array which contains 3x3 matrices chosen from the X.
X = [1 7 9 10 15
4 8 5 9 21
2 14 3 6 16]
A = nchoosek(X(1,:),3)
B = nchoosek(X(2,:),3)
C = nchoosek(X(3,:),3)
for i = 1:nchoosek(5,3)
M{i} = [A(i,:);B(i,:);C(i,:)];
end
See Also
Categories
Find more on Matrices and Arrays 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!