Solving 3 x 9 matrix all posible combinations

1 次查看(过去 30 天)
How can i find every posible combination of 3 x 9 difrent matix [mn] from 3 row matrix [a1,a2,a3]?
for example:
a1=[1,0,0];
a2=[0,1,0];
a3=[0,0,1];
>> m1=[a1;a1;a1;a1;a1;a1;a1;a1;a1]
m1 =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m2=[a2;a1;a1;a1;a1;a1;a1;a1;a1]
m2 =
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m3=[a1;a2;a1;a1;a1;a1;a1;a1;a1]
m3 =
1 0 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m4=[a1;a1;a2;a1;a1;a1;a1;a1;a1]
m4 =
1 0 0
1 0 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m5=[a1;a1;a1;a2;a1;a1;a1;a1;a1]
m5 =
1 0 0
1 0 0
1 0 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m6=[a1;a1;a1;a1;a2;a1;a1;a1;a1]
m6 =
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
>> m7=[a1;a1;a1;a1;a1;a2;a1;a1;a1]
m7 =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
1 0 0
1 0 0
1 0 0
>> m8=[a1;a1;a1;a1;a1;a1;a2;a1;a1]
m8 =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
1 0 0
1 0 0
>> m9=[a1;a1;a1;a1;a1;a1;a1;a2;a1]
m9 =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
1 0 0
>> m10=[a1;a1;a1;a1;a1;a1;a1;a1;a2]
m10 =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
>>m11=[a2;a2;a1;a1;a1;a1;a1;a1;a1]
m11 =
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 0 0
>>m12=[a2;a1;a2;a1;a1;a1;a1;a1;a1]
m11 =
0 1 0
0 0 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 0 0

回答(1 个)

Rik
Rik 2019-8-14
编辑:Rik 2019-8-14
There are 4686825 possible combinations, so I would recommend not storing all of them, especially not in numbered variables, which are a bad idea.
idx=nchoosek(repmat(1:3,1,9),9);
a=cell(1,3);
a{1}=[1,0,0];
a{2}=[0,1,0];
a{3}=[0,0,1];
n=1;
current_combination=cell2mat(a(idx(n,:))');
But if you insist, you can use the code below. Note that it takes a lot of memory and a lot of time (about a 4GB increase in RAM usage and 3-4 minutes on my machine (W10x64,R2019a)).
idx=nchoosek(repmat(1:3,1,9),9);
a=cell(1,3);
a{1}=[1,0,0];
a{2}=[0,1,0];
a{3}=[0,0,1];
total=cellfun(@(n) {cell2mat(a(idx(n,:))')},num2cell(1:size(idx,1)));
total=reshape(total,1,1,[]);
total=cell2mat(total);

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by