generating a subset of specific of unique 3 groups out of 84

Hi all, I've the following indices: indices = nchoosek ([1:9], 3)
indices =
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
1 2 9
1 3 4
1 3 5
1 3 6
1 3 7
1 3 8
1 3 9
1 4 5
1 4 6
1 4 7
1 4 8
1 4 9
1 5 6
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
1 7 8
1 7 9
1 8 9
2 3 4
2 3 5
2 3 6
2 3 7
2 3 8
2 3 9
2 4 5
2 4 6
2 4 7
2 4 8
2 4 9
2 5 6
2 5 7
2 5 8
2 5 9
2 6 7
2 6 8
2 6 9
2 7 8
2 7 9
2 8 9
3 4 5
3 4 6
3 4 7
3 4 8
3 4 9
3 5 6
3 5 7
3 5 8
3 5 9
3 6 7
3 6 8
3 6 9
3 7 8
3 7 9
3 8 9
4 5 6
4 5 7
4 5 8
4 5 9
4 6 7
4 6 8
4 6 9
4 7 8
4 7 9
4 8 9
5 6 7
5 6 8
5 6 9
5 7 8
5 7 9
5 8 9
6 7 8
6 7 9
6 8 9
7 8 9
where each row of the above 84 rows gives a specific value as shown: C1=reshape(C_IA,size(indices,1) ,[])
C1 =
26.5871 - 0.0000i
25.3748 + 0.0000i
29.1875 - 0.0000i
27.8822 - 0.0000i
27.9279 - 0.0000i
29.8068 - 0.0000i
20.1088 + 0.0000i
28.9700 + 0.0000i
31.4866 - 0.0000i
30.7709 - 0.0000i
29.1391 - 0.0000i
29.5271 - 0.0000i
28.7390 - 0.0000i
32.2309 - 0.0000i
26.0994 - 0.0000i
31.6562 - 0.0000i
27.2213 + 0.0000i
19.1752 + 0.0000i
29.0539 + 0.0000i
31.9886 + 0.0000i
28.7934 + 0.0000i
26.9325 + 0.0000i
30.8589 + 0.0000i
26.5858 + 0.0000i
27.5174 + 0.0000i
31.3600 + 0.0000i
29.4643 - 0.0000i
28.1896 - 0.0000i
20.4114 + 0.0000i
25.9653 - 0.0000i
29.2150 - 0.0000i
29.3785 + 0.0000i
25.1405 - 0.0000i
25.2144 - 0.0000i
19.9587 - 0.0000i
27.7516 + 0.0000i
27.5817 + 0.0000i
30.9471 - 0.0000i
21.9452 + 0.0000i
25.7818 - 0.0000i
27.0898 - 0.0000i
25.6516 + 0.0000i
27.9703 - 0.0000i
29.8913 - 0.0000i
28.7567 + 0.0000i
26.5974 - 0.0000i
27.8911 + 0.0000i
28.2247 + 0.0000i
30.4570 - 0.0000i
32.3383 - 0.0000i
23.6659 - 0.0000i
26.0332 - 0.0000i
24.4511 - 0.0000i
31.6553 - 0.0000i
28.5011 + 0.0000i
28.2660 - 0.0000i
25.6020 - 0.0000i
30.5952 + 0.0000i
23.9412 + 0.0000i
18.3079 - 0.0000i
27.3033 + 0.0000i
29.0838 + 0.0000i
29.8749 - 0.0000i
28.0912 + 0.0000i
19.2981 + 0.0000i
23.3627 - 0.0000i
29.4325 - 0.0000i
31.1372 - 0.0000i
22.5496 - 0.0000i
27.3350 - 0.0000i
28.2363 + 0.0000i
34.0017 + 0.0000i
28.1177 - 0.0000i
32.8989 + 0.0000i
28.2460 - 0.0000i
26.2227 - 0.0000i
24.6639 - 0.0000i
25.9026 + 0.0000i
30.5478 - 0.0000i
25.5483 - 0.0000i
29.4994 - 0.0000i
31.3045 + 0.0000i
31.9083 - 0.0000i
32.6127 - 0.0000i
I would like to generate 3 different group of numbers in one time (where each row represent a number) so that all the three groups has unique (not repeated) numbers. i.e. If the 1st generated group was : 1 2 3 (which has a value of 26.5871 then the 2nd group must be any three numbers from 1 to 9 except 1 2 3 (let's say 2nd generated group was: 4 5 6 which has a value of 19.2981 ) then the 3rd generated group must be any number from 1 to 9 except all previously selected numbers (any 3 numbers except 1,2,3,4,5,6) hence we have 3rd group is: 7 8 9 which has a value of 32.6127 so, the expected result of my example is : 26.5871 19.2981 32.6127
I need to do this for all different cases (and not for only the above case), i.e to generate all possible ways of dividing up the numbers from 1 to 9 into three groups with three numbers in each group. The total number of these is 9!/3!/3!/3! = 1680 different result. (the above Example gives only one result out of those 1680 result that i need)
another 2nd example for clarity, if one combination out of the 1680 combinations was: 7 8 9 4 5 6 1 2 3 then i expect to get C1(84,:) and C1(65,:) and C1(1,:)
any help?
P.S. I've already got the 1680 rows combinations but don't know how to map those to my 84 result of C1

 채택된 답변

Roger Stafford
Roger Stafford 2014년 1월 21일
Suppose [i1,i2,i3,i4,i5,i6,i7,i8,i9] is one of your combinations. Then you can reference the corresponding three values in C1 using the three indices k1, k2, and k3 in the following:
n = 9;
k1 = n*(n-1)*(n-2)/6-(n-i1)*(n-1-i1)*(n-2-i1)/6-(n-i2)*(n-1-i2)/2-(n-i3);
k2 = n*(n-1)*(n-2)/6-(n-i4)*(n-1-i4)*(n-2-i4)/6-(n-i5)*(n-1-i5)/2-(n-i6);
k3 = n*(n-1)*(n-2)/6-(n-i7)*(n-1-i7)*(n-2-i7)/6-(n-i8)*(n-1-i8)/2-(n-i9);
and get
C1(k1), C1(k2), C1(k3)
(This formula is in fact valid for any n, not just n = 9.)

추가 답변 (3개)

Andrei Bobrov
Andrei Bobrov 2014년 1월 20일
편집: Andrei Bobrov 2014년 1월 20일
C1 = real(C1);
ii = nchoosek ([1:9], 3);
[~,groups1] = sort(randi(100,10,9),2); % example
[l,i1] = ismember(sort(reshape(groups1',3,[])',2),ii,'rows');
out = reshape(C1(i1(l)),3,[]).';

댓글 수: 1

I'm not sure if you got me right, but I want to get 1680 result (all different combinations) and not a random group example

댓글을 달려면 로그인하십시오.

Jos (10584)
Jos (10584) 2014년 1월 20일
The following generates three arrays with 3 columns and 1680 rows each. Each row corresponds to a unique combination of numbers. Every number from 1 to 9 is present only once among the same rows of Group1, Group2 and Group3:
tf1 = permpos(3,9) ;
tf2 = permpos(3,6) ;
sz1 = size(tf1,1) ; sz2 = size(tf2,1) ;
sz = sz1 * sz2 ; % total size (should be 1680)
tf1 = kron(tf1,ones(sz2,1)).' ; % expand
tf2 = repmat(tf2,sz1,1).' ;
tf2 = 3 - tf2 ;
tf1(tf1==0) = tf2 ; % now tf1 hold group number
% convert to numbers 1:9
x = repmat([1 2 3],sz,1).' ;
tf1 = -tf1 ;
tf1(tf1==-1) = x ;
tf1(tf1==-2) = x+3 ;
tf1(tf1==-3) = x+6 ;
% each row of t1 holds the unique order of numbers
% split (if really needed)
Group1 = tf1(1:3,:).' ;
Group2 = tf1(4:6,:).' ;
Group3 = tf1(7:9,:).' ;
Now you can convert (select?) from C using these indices.

댓글 수: 2

Thanks alot for the code, but my problem is that I don't know how to select from C to get 1680 results. I already have the following code which generate 1680 indices:
C1 = nchoosek([1:K],sel_user);
C1 = [C1,zeros(size(C1,1),6)]; %6 should be 2
x = 1:9; %should be from 1:3
for p = 1:size(C1,1)
t = true(1,9); %9 should be 3
t(C1(p,1:3)) = false(1,3);
C1(p,4:9) = x(t); %should be from 2:3
end
C2 = nchoosek(1:6,3); %
C2 = [C2,zeros(size(C2,1),3)];
x = 1:6; %from 1:2
for q = 1:size(C2,1)
t = true(1,6); t(C2(q,1:3)) = false(1,3);
C2(q,4:6) = x(t);
end
A = zeros(size(C1,1)*size(C2,1),9);
k = 0;
for p = 1:size(C1,1)
x = C1(p,1:3);
y = C1(p,4:9);
for q = 1:size(C2,1)
k = k + 1;
A(k,:) = [x,y(C2(q,:))];
end
end
the matrix A is the 1680 combinations but I want to map the matrix A into my C1 matrix in order to get 1680 result (out from C1)
Create the 84 indices and find the location of the three groups using ismember:
indices84 = nchoosek ([1:9], 3)
[~, Cind1] = ismember(Group1, indices84,'rows') ;
[~, Cind2] = ismember(Group2, indices84,'rows') ;
[~, Cind3] = ismember(Group3, indices84,'rows') ;
AllC = [C(Cind1) C(Cind2) C(Cind3)] ;

댓글을 달려면 로그인하십시오.

Hisham
Hisham 2014년 1월 20일
for clarity, if one combination out of the 1680 combinations was: 7 8 9 4 5 6 1 2 3 then i expect to get C1(84,:) and C1(65,:) and C1(1,:)
any help?
P.S. I've already got the 1680 rows combinations but don't know how to map those to my 84 result of C1

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2014년 1월 20일

댓글:

2014년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by