我正在尝试将数据集拆分为2个组,以便两个组都具有至少一次出现的所有唯一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 :(得分: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
>>