## How to make combinations of given condition?

Asked by C Zeng

### C Zeng (view profile)

on 28 Dec 2012
Accepted Answer by Andrei Bobrov

### Andrei Bobrov (view profile)

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 (view profile)

Answer by Andrei Bobrov

### Andrei Bobrov (view profile)

on 28 Dec 2012
Edited by Andrei Bobrov

### Andrei Bobrov (view profile)

on 28 Dec 2012

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

C Zeng

### C Zeng (view profile)

on 28 Dec 2012

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. :-(

### Roger Stafford (view profile)

Answer by Roger Stafford

### Roger Stafford (view profile)

on 28 Dec 2012

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

C Zeng

on 1 Jan 2013

Thanks, got it!