Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:59:16+00:00 2026-05-25T14:59:16+00:00

I have an array of structs where I perform two searches. First I search

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:59:17+00:00Added an answer on May 25, 2026 at 2:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data represented in CF as an array of structs e.g.: var foo
i have an array of structs and i need to get the size of
I have an array of structs. The struct has two function pointers. Each element
I have an array of structs. Each struct has the following two attributes: win
I have an array of structs and one of the fields in the struct
I have an array of objects that have QTTime structs as attributes. The objects
I have an array of structs that I have defined in a header file:
I have an array of structs in ColdFusion. I'd like to sort this array
I have an array of structs called leaders. The struct class looks like this,
I have any array of structs. Each struct in the array has the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.