How to make combinations of given condition?

1 次查看(过去 30 天)
Hi, all,
For example, if I want to make all combination of 5-dim vectors have only 2 two's, other entries are all 0. Is there a command in Matlab can do it? That is 22000 20200 20020 20002 02200 02020 02002 00220 00202 00022
Thanks.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-12-28
编辑:Andrei Bobrov 2012-12-28
k = dec2bin(0:sum(pow2(0:4)))-'0';
out = 2*k(sum(k,2) == 2,:);
  1 个评论
C Zeng
C Zeng 2012-12-28
Andrei, thanks, can you explain?
Actually I want all other entries to be 1. Is there a way to do it easily?
I tried your code, it works here though not understood here. :-(

请先登录,再进行评论。

更多回答(1 个)

Roger Stafford
Roger Stafford 2012-12-28
This is a problem for matlab's 'nchoosek' function.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by