generating matrices of the possible combinations

1 次查看(过去 30 天)
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 ?

采纳的回答

Stephen23
Stephen23 2018-12-6
编辑:Stephen23 2018-12-6
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

更多回答(1 个)

Luna
Luna 2018-12-6
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

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by