在Matlab中尽可能匹配多个数据集之间的数据
我有四组数据,每组数据都存储为结构数组。 通过数组的不同组合进行逻辑索引,可以找到四个数据集中共有的数据。
问题: 如果我在所有四个数据集中找不到任何相交的数据匹配(=完美匹配),我想找到可以在任何三个数据集中找到的数据。 如果我在三个数据集的任意组合中找不到任何相交的数据匹配,我想在任意两个数据集中找到相交的数据。
当然,由于我只有四个数据集,我可以编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以在这里使用逻辑索引。
matchingB(:)
使用matching(1,:)
和matching(2,:)< 代替
matchingA(:)
/代码>。然后你可以使用当然你可以用元胞数组
mayMatch{N}
替换mayMatchN
并循环N
以使其更加紧凑。Yes, you can use logical indexing here. Instead of
matchingA(:)
,matchingB(:)
usematching(1,:)
andmatching(2,:)
. Then you can useOf course you could replace
mayMatchN
by a cell arraymayMatch{N}
and loop overN
to make it even more compact.