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

  • Home
  • SEARCH
  • 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 3278624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:30:06+00:00 2026-05-17T19:30:06+00:00

I am looking for an efficient way to determine if a set is a

  • 0

I am looking for an efficient way to determine if a set is a subset of another set in Matlab or Mathematica.

Example:
Set A = [1 2 3 4]
Set B = [4 3]
Set C = [3 4 1]
Set D = [4 3 2 1]

The output should be: Set A

Sets B and C belong to set A because A contains all of their elements, therefore, they can be deleted (the order of elements in a set doesn’t matter). Set D has the same elements as set A and since set A precedes set D, I would like to simply keep set A and delete set D.

So there are two essential rules:
1. Delete a set if it is a subset of another set
2. Delete a set if its elements are the same as those of a preceding set

My Matlab code is not very efficient at doing this – it mostly consists of nested loops.

Suggestions are very welcome!

Additional explanation: the issue is that with a large number of sets there will be a very large number of pairwise comparisons.

  • 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-17T19:30:07+00:00Added an answer on May 17, 2026 at 7:30 pm

    You will likely want to take a look at the built-in set operation functions in MATLAB. Why reinvent the wheel if you don’t have to? 😉

    HINT: The ISMEMBER function may be of particular interest to you.

    EDIT:

    Here’s one way you can approach this problem using nested loops, but setting them up to try and reduce the number of potential iterations. First, we can use the suggestion in Marc‘s comment to sort the list of sets by their number of elements so that they are arranged largest to smallest:

    setList = {[1 2 3 4],...  %# All your sets, stored in one cell array
               [4 3],...
               [3 4 1],...
               [4 3 2 1]};
    nSets = numel(setList);                       %# Get the number of sets
    setSizes = cellfun(@numel,setList);           %# Get the size of each set
    [temp,sortIndex] = sort(setSizes,'descend');  %# Get the sort index
    setList = setList(sortIndex);                 %# Sort the sets
    

    Now we can set up our loops to start with the smallest sets at the end of the list and compare them first to the largest sets at the start of the list to increase the odds we will find a superset quickly (i.e. we’re banking on larger sets being more likely to contain smaller sets). When a superset is found, we remove the subset from the list and break the inner loop:

    for outerLoop = nSets:-1:2
      for innerLoop = 1:(outerLoop-1)
        if all(ismember(setList{outerLoop},setList{innerLoop}))
          setList(outerLoop) = [];
          break;
        end
      end
    end
    

    After running the above code, setList will have all sets removed from it that are either subsets or duplicates of other sets preceding them in the list.

    In the best case scenario (e.g. the sample data in your question) the inner loop breaks after the first iteration every time, performing only nSets-1 set comparisons using ISMEMBER. In the worst case scenario the inner loop never breaks and it will perform (nSets-1)*nSets/2 set comparisons.

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

Sidebar

Related Questions

No related questions found

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.