Solving 3 x 9 matrix all posible combinations

7 views (last 30 days)
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

Answers (1)

Rik
Rik on 14 Aug 2019
Edited: Rik on 14 Aug 2019
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);

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!