我有一系列结构,我执行两次搜索。 首先,我搜索特定颜色,然后搜索特定城市。 我得到两个包含我正在寻找的数据的数据集。 到目前为止,没有问题。
从我得到的两个数据集中,我想找到两个数据集中存在的两个数据集中的结构。
我试过'相交',因为这似乎是数组的一个很好的选择。 但我似乎没有得到任何相交的数据......为什么不呢?
代码看起来像这样:
%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); endI 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.
更多推荐
发布评论