File Exchange

## Picking elements from a set

version 1.0 (3.83 KB) by

Gives all possibilities of picking k elements from a set of N with/without order/repetition.

Updated

s = pick(V,k,Type)

Gives all possibilities of picking k elements from the set V with or without order and repetition.
V can be an array of any size and any type.
Type can have the following values: '', 'o', 'r', 'or'. 'o' means pick ordered list, 'r' means replace elements after picking.

s is an array with all picks, one per row.

Examples:

>> pick(0:1, 4, 'or')
ans =
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
...

>> pick('abcd', 2, '')
ans =
ab
ac
bc
bd
cd

>> pick(1:3,3,'o')
ans =
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Denis Vasilev

JP

Omar

Michele Colombo

### Michele Colombo (view profile)

very Useful, precisely what i was looking for. i appreciate the flxibility with/without repetition

You help me a lot, this is really exactly what I want

N. Al-Azri

Bless you Stefan,

Ilya Kuprov

You'd never guess what I use it for -- my spectroscopists are unsure about the signal assignment, so I have to scan the entire assgnment space just in case! :)

Ryan held

Thanks - very helpful, nice implementation

Jan Peters

Great, exactly what I was looking for, thanks!

Dag Lyberg

Just what I needed: there is no function for permutations with replacement in Matlab.

Morris Z

Thank you so much! Great function!

Uwe Friederich

simple but good!!

Stefan stefan.stoll@ethz.ch

PICK adds the following compared to NCHOOSEK + COMBN:
- ordered list without repeating elements
- unordered list with repeating elements
- a common interface to all four "picking problems"

PICK(V,N,'or') is faster than COMBN(V,N)
for larger V and N. COMBN(V,N) is faster
for small V and N.

Jos NOjos@SPAMjasen.nl

What does this add to nchoosek(V,N) (in Matlab) and combn(V,N) (here on the FEX)? (may be in combination with a sort for ordered lists).
At least this submission is much slower.

MATLAB 7 (R14)