MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by C Zeng
on 28 Dec 2012

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.

*No products are associated with this question.*

Answer by Andrei Bobrov
on 28 Dec 2012

Edited by Andrei Bobrov
on 28 Dec 2012

Accepted answer

k = dec2bin(0:sum(pow2(0:4)))-'0'; out = 2*k(sum(k,2) == 2,:);

Answer by Roger Stafford
on 28 Dec 2012

This is a problem for matlab's 'nchoosek' function.

## 0 Comments