【问题标题】:Filename matching between two datasets两个数据集之间的文件名匹配
【发布时间】:2017-10-28 10:59:31
【问题描述】:

我有一个在 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 匹配。

【问题讨论】:

  • 抱歉,我正忙于查看我的答案,但我认为 @gnovice 答案需要你所需要的。

标签: matlab join merge


【解决方案1】:

这与 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); 

【讨论】:

  • 也可以参考这个thread
  • 在我的情况下,set 1 和 set 2 没有相同的变量(只有少数变量是相同的),因此在使用 ismember 时出现错误。
  • @DaphneMariaravi 他们不应该是一样的。它仅应用于文件名。错误是什么,在哪一行?
  • 我收到一条错误消息,指出“使用表格/ismember 时出错(第 45 行)A 和 B 必须包含相同的变量。”
  • @DaphneMariaravi 提供代表您的数据的示例数据集,然后我可以帮助您。根据您的问题,这对我有用。
【解决方案2】:

如果您想在两个数据集之间比较与给定文件关联的最大值,在这种情况下您可以不必担心标签。我将使用您问题中的两个示例数据集作为示例(假设这些是tables 的数据,例如your last question):

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找到最大值:

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

现在我们可以用表格来可视化数据:

T3 = table(commonFiles, max1, max2);

T3 = 

    commonFiles    max1    max2
    ___________    ____    ____

    'yyy'          4       4   
    'zzz'          7       7

在此示例中,两组中每个文件的最大值相同。如果您只想专注于不同的部分,您可以这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2015-08-10
    • 1970-01-01
    • 2018-07-12
    • 2016-12-09
    相关资源
    最近更新 更多