在Matlab中尽可能匹配多个数据集之间的数据

发布于 2024-12-05 22:04:47 字数 1890 浏览 0 评论 0原文

我有四组数据,每组数据都存储为结构数组。 通过数组的不同组合进行逻辑索引,可以找到四个数据集中共有的数据。

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

当然,由于我只有四个数据集,我可以编写 if 子句来分别处理每个组合情况,这需要一段时间,但应该是可能的。 但由于我使用的是 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

I have four sets of data, each of which is stored as an array of structs.
The data that is common in the four data sets can be found by performing logical indexing with different combinations of the arrays.

The problem:
If I cannot find any intersected data matches in all four data sets (= a perfect match), I would like to find the data that can be found in any three data sets.
If I cannot find any intersected data matches in any combination of three data sets, I would like to find intersected data in any two data sets.

Of course, since I only have four data sets, I could write if-clauses to handle each combination case separately, it will take a while but it should be possible.
But since I am using Matlab I am curious if a smart Matlabish way for handeling this exists?
What would be a feasible way of doing this if I had had ten datasets to be processed?

Maybe it is possible to generate an array with all the combinations and transform the array entries into logical indexing expressions?

The output of the program will be something like this:

The following items matched all criteria:
...
The following items matched only criteria A, B, C:
...
The following items matched only A and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-12-12 22:04:47

是的,您可以在这里使用逻辑索引。 matchingB(:) 使用 matching(1,:)matching(2,:)< 代替 matchingA(:) /代码>。然后你可以使用

%#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}替换mayMatchN并循环N以使其更加紧凑。

Yes, you can use logical indexing here. Instead of matchingA(:), matchingB(:) use matching(1,:) and matching(2,:). Then you can use

%#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

Of course you could replace mayMatchN by a cell array mayMatch{N} and loop over N to make it even more compact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文