【问题标题】:Matlab: Matrix with all combinations of 0s and 1s with at least k 1s in each rowMatlab:具有0和1的所有组合的矩阵,每行至少有k个1
【发布时间】:2016-11-13 13:19:28
【问题描述】:

所以我选择一个行长,比如 n,每行将只包含 0 和 1,并且至少有 k 个 1。我想在 Matlab 中有一个包含所有可能组合的矩阵。

例如,n=3 k=2:

1 1 0

1 0 1

0 1 1

1 1 1

【问题讨论】:

  • 你试过了吗?我怀疑 Matlab 中是否存在这样的原语,因此您必须编写一个脚本,并因此提供您尝试过的示例。

标签: matlab matrix combinations combinatorics


【解决方案1】:

您可以使用dec2bin 创建所有位模式,然后仅保留具有正确数量的1s 的模式:

n = 3; k = 2;
allCombs = dec2bin( (2^k-1):(2^n-1) ) - '0'; % use -'0' to convert to integers
outCombs = allCombs(sum(allCombs, 2) >= k, :);

outCombs =

   0   1   1
   1   0   1
   1   1   0
   1   1   1

【讨论】:

  • 辉煌._____
猜你喜欢
  • 2021-11-17
  • 1970-01-01
  • 2011-04-17
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多