2017-05-27 124 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));