2011-09-19 118 views
1

我有四组数据,每组数据都存储为一个结构数组。 通过对数组的不同组合执行逻辑索引,可以找到四个数据集中常见的数据。尽可能在多个数据集之间进行数据匹配在Matlab中

问题: 如果我在所有四个数据集(=完美匹配)中找不到任何相交的数据匹配,我想查找可以在任何三个数据集中找到的数据。 如果在三个数据集的任意组合中找不到任何相交的数据匹配,我希望在任何两个数据集中找到相交的数据。

当然,因为我只有四个数据集,所以我可以编写if-clause来分别处理每个组合案例,但这需要一段时间,但它应该是可能的。 但是,因为我使用Matlab,我很好奇,如果一个聪明的Matlabish方式来处理这个问题存在? 如果我有十个要处理的数据集,这样做会是一种可行的方法吗?

也许有可能生成一个包含所有组合的数组,并将数组条目转换为逻辑索引表达式?

程序的输出将是这样的:

下列项目符合所有标准: ... 匹配以下项目唯一标准A,B,C: ... 以下匹配只有A和d项目: ...

%Four sets of data. A number of arrays of structs matching items in ArrayOfStructs 
matchingA = strcmpi({ArrayOfStructs.A},iA); 
matchingB = strcmpi({ArrayOfStructs.B},iB); 
matchingC = strcmpi({ArrayOfStructs.C},iC); 
matchingD = strcmpi({ArrayOfStructs.D},iD); 

%Find as many posts as possible where the posts are identical between the data sets. 

%Try to find posts matching in all four data sets 
if (sum(matchingA)>0 && sum(matchingB)>0 && sum(matchingC)>0 && sum(matchingD)>0) 
      mayMatchAll = ArrayOfStructs(matchingA & matchingB & matchingC & matchingD); 
%etc... 
%Try to find posts matching in any three of the data sets 
%Try to find in A,B,C    
if (sum(matchingA)>0 && sum(matchingB)>0 && sum(matchingC)>0) 
      mayMatch3_1st = ArrayOfStructs(matchingA & matchingB & matchingC); 
%etc... 
%Try to find in A,B,D 
if (sum(matchingA)>0 && sum(matchingB)>0 && sum(matchingD)>0) 
      mayMatch3_2nd = ArrayOfStructs(matchingA & matchingB & matchingD); 
%etc... 
%Try to find in A,C,D 
if (sum(matchingA)>0 && sum(matchingC)>0 && sum(matchingD)>0) 
      mayMatch3_3rd = ArrayOfStructs(matchingA & matchingC & matchingD); 
%etc...    
      %... 

%Try to find posts matching in any two of the data sets 
      %etc... 
end 

回答

0

是的,你可以在这里使用逻辑索引。而不是matchingA(:),matchingB(:)使用matching(1,:)matching(2,:)。然后你可以使用

%#Try to find posts matching in all four data sets 
if any(all(matching)) 
      mayMatchAll = ArrayOfStructs(all(matching)); 
end 
%#Try to find posts matching in any three of the data sets 
if any(sum(matching)==3) 
      mayMatch3 = ArrayOfStructs(sum(matching)==3); 
end 
%#Try to find posts matching in any two of the data sets 
if any(sum(matching)==2) 
      mayMatch2 = ArrayOfStructs(sum(matching)==2); 
end 

当然,你可以通过一个单元阵列mayMatch{N}和环比N更换mayMatchN,使其更加紧凑。

相关问题