An efficient code to determine if a set is a subset of another set

前端 未结 4 2377
悲哀的现实
悲哀的现实 2021-02-20 04:27

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

4条回答
  •  自闭症患者
    2021-02-20 04:57

    I think the question you mean to ask is "given a list of sets, pick out the set that contains all of the others". There are a bunch of edge cases where I don't know what output you would want (e.g. A = { 1, 2 } and B = { 3, 4 }), so you need to clarify a lot.

    However, to answer the question you did ask, about set containment, you can use set difference (equivalently complement wrt another set). In Mathematica, this sort of thing:

    setA = {1, 2, 3, 4};
    setB = {4, 3};
    setC = {3, 4, 1};
    setD = {4, 3, 2, 1};
    Complement[setD, setA] == {}
     True
    

    indicates setD is a subset of setA.

提交回复
热议问题