在MATLAB中找到两个结构数组的交集(Find the intersection of two arrays of structs in MATLAB)

编程入门 行业动态 更新时间:2024-10-19 04:27:42
在MATLAB中找到两个结构数组的交集(Find the intersection of two arrays of structs in MATLAB)

我有一系列结构,我执行两次搜索。 首先,我搜索特定颜色,然后搜索特定城市。 我得到两个包含我正在寻找的数据的数据集。 到目前为止,没有问题。

从我得到的两个数据集中,我想找到两个数据集中存在的两个数据集中的结构。

我试过'相交',因为这似乎是数组的一个很好的选择。 但我似乎没有得到任何相交的数据......为什么不呢?

代码看起来像这样:

%Array of structs InfoArray(1) = struct ('Name','AAAA', 'City', 'London', 'Test', '70', 'FavouriteColor', 'red'); InfoArray(2)= struct('Name','BBBB', 'City', 'London', 'Test', '20', 'FavouriteColor', 'blue'); InfoArray(3)= struct('Name','CC', 'City', 'London', 'Test', '10', 'FavouriteColor', 'white'); InfoArray(4)= struct('Name','DD', 'City', 'Stockholm', 'Test', '30', 'FavouriteColor', 'yellow'); InfoArray(5)= struct('Name','EEEEE', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(6)= struct('Name','FFFF', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(7)= struct('Name','GG', 'City', 'Stockholm', 'Test', '80', 'FavouriteColor', 'blue'); InfoArray(8)= struct('Name','H', 'City', 'Oslo', 'Test', '60', 'FavouriteColor', 'pink'); InfoArray(9)= struct('Name','III', 'City', 'Oslo', 'Test', '5', 'FavouriteColor', 'red'); InfoArray(10)= struct('Name','JJJJ', 'City', 'Stockholm', 'Test', '40', 'FavouriteColor', 'blue'); InfoArray(11)= struct('Name','KKKK', 'City', 'London', 'Test', '70', 'FavouriteColor', 'white'); %Find structs in array with color: 'red' iColor = 'red'; [pFound,matchingFavouriteColors] = findPost(InfoArray,'FavouriteColor',iColor); %Find structs in array with City: 'London' iCity = 'London'; [pFound,matchingCity] = findPost(InfoArray,'City',iCity); %Find the structs that are found in both of the data sets ???? [c, ia, ib] = intersect(matchingFavouriteColors, matchingCity); disp([c; ia; ib]) function [matchFound, matchingData] = findPost(db,sField,iField) matches = find(strcmpi({db.(sField)},iField)); if(isempty(matches)) disp('No matches found'); postsFound=0; else matchingData = db(matches(:)); matchFound=length(matches); end

I have an array of structs where I perform two searches. First I search for a specific colour and then I search for a specific city. I get two data sets containing the data I was looking for. So far, no problems.

From the two data sets I get, I would like to find the structs in the two data sets that are present in both data sets.

I have tried 'intersect', as this seemed to be a good option for arrays. But I do not seem not get any intersected data... Why not?

The code looks something like this:

%Array of structs InfoArray(1) = struct ('Name','AAAA', 'City', 'London', 'Test', '70', 'FavouriteColor', 'red'); InfoArray(2)= struct('Name','BBBB', 'City', 'London', 'Test', '20', 'FavouriteColor', 'blue'); InfoArray(3)= struct('Name','CC', 'City', 'London', 'Test', '10', 'FavouriteColor', 'white'); InfoArray(4)= struct('Name','DD', 'City', 'Stockholm', 'Test', '30', 'FavouriteColor', 'yellow'); InfoArray(5)= struct('Name','EEEEE', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(6)= struct('Name','FFFF', 'City', 'Oslo', 'Test', '15', 'FavouriteColor', 'red'); InfoArray(7)= struct('Name','GG', 'City', 'Stockholm', 'Test', '80', 'FavouriteColor', 'blue'); InfoArray(8)= struct('Name','H', 'City', 'Oslo', 'Test', '60', 'FavouriteColor', 'pink'); InfoArray(9)= struct('Name','III', 'City', 'Oslo', 'Test', '5', 'FavouriteColor', 'red'); InfoArray(10)= struct('Name','JJJJ', 'City', 'Stockholm', 'Test', '40', 'FavouriteColor', 'blue'); InfoArray(11)= struct('Name','KKKK', 'City', 'London', 'Test', '70', 'FavouriteColor', 'white'); %Find structs in array with color: 'red' iColor = 'red'; [pFound,matchingFavouriteColors] = findPost(InfoArray,'FavouriteColor',iColor); %Find structs in array with City: 'London' iCity = 'London'; [pFound,matchingCity] = findPost(InfoArray,'City',iCity); %Find the structs that are found in both of the data sets ???? [c, ia, ib] = intersect(matchingFavouriteColors, matchingCity); disp([c; ia; ib]) function [matchFound, matchingData] = findPost(db,sField,iField) matches = find(strcmpi({db.(sField)},iField)); if(isempty(matches)) disp('No matches found'); postsFound=0; else matchingData = db(matches(:)); matchFound=length(matches); end

最满意答案

什么错误信息相交给你? 这应该会给你一个提示,说明为什么它不起作用。

要完成你想要的任务,你不需要你的findPost函数(它有一个在postsFound=0;什么都不做的赋值postsFound=0;以及一个误导性命名的变量matchFound ,顺便说一句。),你可以使用逻辑索引。

iRed = strcmpi({InfoArray.FavouriteColor},'red'); iLondon = strcmpi({InfoArray.City},'London'); InfoArray(iRed & iLondon)

iRed包含1 s,颜色为红色, iLondon位于城市为伦敦的索引处, iRed & iLondon确切iRed & iLondon两者都是真的 - 这些逻辑数组可用作结构数组的索引。

编辑 :或者,您可以获取数字索引(即find(strcmpi({db.(sField)},iField))结果find(strcmpi({db.(sField)},iField))并对它们使用intersect ,将数字索引获取到所需的数组元素,但这似乎有点......间接的。

What error message does intersect give you? That should give you a hint why it does not work.

To accomplish what you want, you do not need your findPost function (which has an assignment that does nothing at postsFound=0; and a misleadingly named variable matchFound, btw.), you can use logical indexing.

iRed = strcmpi({InfoArray.FavouriteColor},'red'); iLondon = strcmpi({InfoArray.City},'London'); InfoArray(iRed & iLondon)

iRed contains 1s exactly where the color is red, iLondon at the indices where the city is london, and iRed & iLondon exactly where both are true -- and these logical arrays can be used as index to your struct array.

Edit: Alternatively, you could get hold of the numeric indices (i.e. the results of find(strcmpi({db.(sField)},iField)) and use intersect on them, getting the numeric indices to the array elements you want, but this seems a bit ... indirect.

更多推荐

本文发布于:2023-08-02 08:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   两个   结构   中找到   MATLAB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!