2017-05-27 50 views
1

我有一個用MATLAB加載的帶有常用標籤的文件列表。在MATLAB中匹配的文件名

label filename A B 
    1  xxx  6 
    1  xxx  2 
    1  xxx  3 
    2  yyy  1 
    2  yyy  4 
    3  zzz  6 
    3  zzz  7 

我還有一個批處理文件的方式如下:

filename  A  B 
    yyy  1 
    yyy  4 
    aaa  2 
    aaa  4 
    aaa  6 
    aaa  10 
    zzz  6 
    zzz  7 

我需要在組1匹配文件名,並設置2,並把相同的標籤設置2(數字)。匹配標籤的目的是set_1中的標籤對於每個文件名在列A中具有最大值。現在在類似的戰爭中,我需要找出set_2中每個文件名的最大值是否與set_2匹配。 有什麼建議嗎?

+0

對不起,我太忙了審查我的答案,但我認爲@gnovice回答希望你所需要的。 – Masoud

回答

0

這和excel中的「VLookUp」是一樣的。您可以使用ismember

[finder, indx] = ismember(Set2(:, 1), Set1(:, 2)); 

%Label will be in the 3rd column of Set2 (pre-allocation is needed): 
    Set2(finder, 3) = Set1(indx(finder), 1); 
+0

另請參閱此[線程](https://stackoverflow.com/questions/20643364/matlab-make-matrix-from-vectors-with-gaps) – Masoud

+0

在我的情況下,set 1和set 2不具有相同的變量(只有幾個變量是相同的),因此我在使用ismember時出錯。 – DaphFab

+0

@DaphneMariaravi他們不應該是一樣的。它僅應用於文件名。什麼是錯誤和什麼線? – Masoud

0

如果你想比較兩個數據集之間的給定文件相關聯的最大值,可以放棄不用擔心在這種情況下的標籤。我將使用兩個樣本數據集從你的問題的例子在這裏(假設這些都是像your last question數據tables):

T1 = table([1; 1; 1; 2; 2; 3; 3], ... 
      {'xxx'; 'xxx'; 'xxx'; 'yyy'; 'yyy'; 'zzz'; 'zzz'}, ... 
      [6; 2; 3; 1; 4; 6; 7], ... 
      'VariableNames', {'Label', 'Filename', 'A'}); 
T2 = table({'yyy'; 'yyy'; 'aaa'; 'aaa'; 'aaa'; 'aaa'; 'zzz'; 'zzz'}, ... 
      [1; 4; 2; 4; 6; 10; 6; 7], ... 
      'VariableNames', {'Filename', 'A'}); 

首先,你可以使用intersect獲得兩個表共用的文件列表。然後使用ismember找到了共同文件的索引中各設置成使用累積值,並與accumarray找到最大:

T3 = table(commonFiles, max1, max2); 

T3 = 

    commonFiles max1 max2 
    ___________ ____ ____ 

    'yyy'   4  4 
    'zzz'   7  7 

commonFiles = intersect(T1.Filename, T2.Filename); 

[index, accumIndex] = ismember(T1.Filename, commonFiles); 
max1 = accumarray(accumIndex(index), T1.A(index), [], @max); % Max values for set 1 

[index, accumIndex] = ismember(T2.Filename, commonFiles); 
max2 = accumarray(accumIndex(index), T2.A(index), [], @max); % Max values for set 2 

現在我們可以用表格可視化數據在這個例子中,兩組中的每個文件的最大值是相同的。如果你想只專注於爲不同的人,你可以這樣做:

index = (max1 ~= max2); % Index of differing maxima 
T3 = table(commonFiles(index), max1(index), max2(index));