2016-06-07 208 views
0

我想將數據集分成兩組,這樣兩組都具有至少一次所有唯一ID。該數據集是一樣的東西在Matlab中將數據分成兩組,分別具有所有唯一ID

01 02 03 04 05 06 07 
07 05 08 09 10 11 12 
01 04 07 13 08 14 15 
06 10 11 12 08 01 02 
13 14 10 01 07 03 02 
15 01 03 04 10 13 11 
11 12 03 05 07 14 15 
06 05 10 13 01 09 14 

我試圖用Matlab將其分割在2個大致相等組,使得兩組至少有一個排在那裏的唯一ID(在這種情況下,01 - 15)存在至少一次。感謝任何幫助完成此項任務。

的數據具有的方式,整個行必須屬於任何一組1或組2我在看我的輸出爲要被劃分2個矩陣,使得

01 02 03 04 05 06 07 
07 05 08 09 10 11 12 
01 04 07 13 08 14 15 
06 10 11 12 08 01 02 

13 14 10 01 07 03 02 
15 01 03 04 10 13 11 
11 12 03 05 07 14 15 
06 05 10 13 01 09 14 

是2個輸出組。

01 02 03 04 05 06 07是第1行。07 05 08 09 10 11 12是第2行等等。每行有7個ID。有8個不同的行。我想把它分成兩組,每組都有5/4行(小變化不重要)。每行中ID的位置不能改變。每行必須作爲整體發送到組1或2,但行結構(該行中每個id的位置)必須保持不變。所有獨特的ID都需要出現在這兩個組中。

+0

什麼如果ID不存在的至少兩倍?如果數據點的數量不能被2整除呢? – excaza

+0

另外我不知道你的輸出標準是什麼意思。輸出的形狀是什麼?它與輸入有什麼關係? – excaza

+0

每個ID至少出現兩次。我已經檢查過。此外,輸出結果只是從上述數據中獲得的兩組。 – user1434997

回答

1

腳本

clear;clc 

A = [01 02 03 04 05 06 07; 
07 05 08 09 10 11 12; 
01 04 07 13 08 14 15; 
06 10 11 12 08 01 02; 
13 14 10 01 07 03 02; 
15 01 03 04 10 13 11; 
11 12 03 05 07 14 15; 
06 05 10 13 01 09 14]; 

% find unique elements and rows containing them 
UniqElem = unique(A); 
NUniqElem = length(UniqElem); 
UniqIndex = struct('UniqElem', cell(NUniqElem,1), ... 
    'UniqRows', cell(NUniqElem, 1), 'RowCount', cell(NUniqElem, 1)); 
for ii = 1:NUniqElem 
    t1 = UniqElem(ii); 
    t2 = find(any(A==t1,2)); 
    UniqIndex(ii).UniqRows = t2; 
    UniqIndex(ii).UniqElem = t1; 
    UniqIndex(ii).RowCount = length(t2); 
end 
clear('t1','t2') 

% find all possible combinations to make the first group 
Combs1 = testf(UniqIndex); 
Combs2 = struct('Combination', Combs1, ... 
    'Unique', {true}); 
for ii = 1:(length(Combs2)-1) 
    if Combs2(ii).Unique 
     CurrentComb = Combs2(ii).Combination; 
     for jj = (ii+1):length(Combs2) 
      if Combs2(jj).Unique && ... 
        all(ismember(CurrentComb,Combs2(jj).Combination)) 
       Combs2(jj).Unique = false; 
      end 
     end 
    end 
end 
Combs3 = Combs2([Combs2.Unique]); 
Combs4 = struct('Grp1', {Combs3.Combination}, 'Grp2', []); 
AllRows = 1:size(A,1); 
for ii = 1:length(Combs4) 
    Combs4(ii).Grp2 = AllRows(~ismember(AllRows, Combs4(ii).Grp1)); 
end 
Combs5 = struct('Grp1', [], 'Grp2', []); 
for ii = 1:length(Combs4) 
    if all(ismember(UniqElem, unique(A([Combs4(ii).Grp2], :)))) 
     Combs5(end+1) = Combs4(ii); 
    end 
end 

Combinations = Combs5; 
for ii = 1:length(Combinations) 
    fprintf('Solution %d of %d \n', ii, length(Combinations)) 
    CurrentComb = Combinations(ii); 
    fprintf('Group 1 \n') 
    for jj = 1:length(CurrentComb.Grp1) 
     fprintf('R%2d: %s \n', CurrentComb.Grp1(jj), ... 
      num2str(A(CurrentComb.Grp1(jj), :), '%-4d')) 
    end 
    fprintf('Group 2 \n') 
    for jj = 1:length(CurrentComb.Grp2) 
     fprintf('R%2d: %s \n', CurrentComb.Grp2(jj), ... 
      num2str(A(CurrentComb.Grp2(jj), :), '%-4d')) 
    end 
    fprintf('\n') 
end 

功能

function Comb = testf(UniqRowIn) 
if length(UniqRowIn) == 1 
    Comb = num2cell(UniqRowIn.UniqRows)'; 
else 
    t2 = testf(UniqRowIn(2:end)); 
    t1 = UniqRowIn(1).UniqRows; 
    Comb = cell(0); 
    for ii = 1:length(t2) 
     CurrentComb = t2{ii}; 
     if isempty(intersect(CurrentComb, t1)) 
      for jj = 1:length(t1) 
       Comb{end+1,1} = sort([CurrentComb, t1(jj)]); 
      end 
     else 
      Comb{end+1,1} = CurrentComb; 
     end 
    end 
end 
end 

輸出

Solution 1 of 12 
Group 1 
Group 2 

Solution 2 of 12 
Group 1 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 3: 1 4 7 13 8 14 15 
Group 2 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 

Solution 3 of 12 
Group 1 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 6: 15 1 3 4 10 13 11 
R 7: 11 12 3 5 7 14 15 

Solution 4 of 12 
Group 1 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 6: 15 1 3 4 10 13 11 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 5: 13 14 10 1 7 3 2 
R 7: 11 12 3 5 7 14 15 

Solution 5 of 12 
Group 1 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 

Solution 6 of 12 
Group 1 
R 1: 1 2 3 4 5 6 7 
R 3: 1 4 7 13 8 14 15 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 2: 7 5 8 9 10 11 12 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 

Solution 7 of 12 
Group 1 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 
Group 2 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 

Solution 8 of 12 
Group 1 
R 2: 7 5 8 9 10 11 12 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 3: 1 4 7 13 8 14 15 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 

Solution 9 of 12 
Group 1 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 6: 15 1 3 4 10 13 11 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 3: 1 4 7 13 8 14 15 
R 7: 11 12 3 5 7 14 15 

Solution 10 of 12 
Group 1 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 6: 15 1 3 4 10 13 11 
R 7: 11 12 3 5 7 14 15 
Group 2 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 5: 13 14 10 1 7 3 2 
R 8: 6 5 10 13 1 9 14 

Solution 11 of 12 
Group 1 
R 4: 6 10 11 12 8 1 2 
R 6: 15 1 3 4 10 13 11 
R 7: 11 12 3 5 7 14 15 
R 8: 6 5 10 13 1 9 14 
Group 2 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 3: 1 4 7 13 8 14 15 
R 5: 13 14 10 1 7 3 2 

Solution 12 of 12 
Group 1 
R 1: 1 2 3 4 5 6 7 
R 2: 7 5 8 9 10 11 12 
R 5: 13 14 10 1 7 3 2 
R 7: 11 12 3 5 7 14 15 
Group 2 
R 3: 1 4 7 13 8 14 15 
R 4: 6 10 11 12 8 1 2 
R 6: 15 1 3 4 10 13 11 
R 8: 6 5 10 13 1 9 14 

>>