2011-09-19 24 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,使其更加緊湊。

相關問題