【问题标题】:All possible subsets of a given set MATLABAll possible subsets of a given set MATLAB
【发布时间】:2022-12-01 22:27:30
【问题描述】:

Hello I am trying to write a function in Matlab that takes a whole set and outputs all possible subsets of a given set.

For example: If I have a set

A = {1,2}

All possible subsets are

B = { {}, {1}, {2}, {1,2} }

I have tried nchoosek

Any ideas? Kinda lost on how I should implement this?

【问题讨论】:

  • Why do you need all possible sets? Obtaining all those is either trivial or impossible. Trivial in case of a few elements, like you have, impossible for more than (roughly) 12 elements, given it grows factorially. You cannot possibly process all of those, let alone store in your RAM.

标签: matlab


【解决方案1】:

Given set S with n elements, there are 2^n subsets, including the empty set {}. The function nchoosek is the appropriate way to perform this operation. You can try the script below. It does not display the empty set.

set = [1,2,3,4,5]

for i=1:length(set):
    disp(nchosek(set, i))

【讨论】:

  • OP already mentions nchoosek. Plus, as I said in my comment and you mention here: this will create a ridiculously large number of sets very quickly, thus it's infeasible for all but the trivially small sets.
  • It is computationally possible, although your judgment ridicules the effort. I find it very unpleasing.
  • It's computationally possible for n very small (the docs suggest up to n=10). As soon as n=30 you'll need more computational power than available in the biggest clusters.
  • Great. It seems a great application for generators of low cardinality.
猜你喜欢
  • 2022-12-27
  • 2022-12-02
  • 2022-12-02
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2022-12-02
  • 2010-09-23
相关资源
最近更新 更多