File Exchange

image thumbnail

Picking elements from a set

version (1.46 KB) by Stefan Stoll
Gives all possibilities of picking k elements from a set of N with/without order/repetition.

1 Download

Updated 23 Oct 2006

No License

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.


>> 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 =

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

Cite As

Stefan Stoll (2021). Picking elements from a set (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (17)

Ashari Muhri

God bless you, this is certainly useful for my current project! Thanks :)

Nirupam Roy

Denis Vasilev



Michele Colombo

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

Adam Wu

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!!


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.


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 Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: VChooseK, VChooseKR, VChooseKO

Community Treasure Hunt

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

Start Hunting!