File Exchange

image thumbnail


version 1.0 (2.12 KB) by

all possible permutations of M values in N positions

11 Ratings



View License

PERMPOS - all possible permutations of M values in N positions
B = PERMPOS(M,N) where M and N are non-negative integers produces a
logical (N!/M!(N-M)!)-by-N matrix in which each row contains a unique
permation of M trues and (N-M) falses. Note that each column will hence
contain (N-1) trues.

B = PERMPOS(V,N) where V is a vector of length M produces a matrix
where each row a contains the values of V in preserved order, but
uniquely permuted at the N columns. The remaining positions are zero.

permpos(2,4) % ->
% 1 1 0 0
% 1 0 1 0
% 1 0 0 1
% 0 1 1 0
% 0 1 0 1
% 0 0 1 1

permpos([-4 9 3],4) % >
% -4 9 3 0
% -4 9 0 3
% -4 0 9 3
% 0 -4 9 3

and COMBN, ALLCOMB, BALLATSQ, NONES (Matlab File Exchange)

Version 2.1 (march 2007)

Comments and Ratings (14)

Nice code! Thanks


Manoj (view profile)

Absolutely brilliant piece of code

Jenn Lee

I'm loving this code, but when I use (16,32) it starts to stall out, is there a way that this can be parallelized so that I can run it on a remote cluster?


Sam (view profile)

Nima Moshtagh

Richard Crozier

Very useful, thanks.


Anders (view profile)

Exactly what is needed when finding syndromes for the corresponding bit error patterns when making a decoder for block codes

Jos (10584)

Jos (10584) (view profile)

Amanda, could you contact me directly? See any of my recent submissions for an address. Jos


Amanda (view profile)

I've been struggling with a matlab problem of combinations and permutations for a while and came across your post here. I thought you might be able to help me out.

Let's say I have a vector say A =[1 2 3 4] and want to find every way I can choose all the elements of A to fit in some number of bins (let's say 3). Bin 1 could have (1, 2, 3), Bin 2 could have (4) or (0) and Bin 3 could have (4) or (0) as well. The problem isn't quite solved by "nchoosek" or "permpos" because each bin could have more or less elements than the previous bin and the element choice for each bin depends on the elements not already chosen. I know that the number of possible combinations is # of elements in A^#of bins (in this case 81), because I solved it out by hand. I am looking for an easier way to do this! I'm sorry if this sounds confusing!

If you can point me in the right direction or offer me any help I would greatly appreciate it!

ahmad ababnah

This code is so good.

Berta Neto

It does exactly what I was needing.

brad mcevoy

does exactly what i'm looking for. good job and thanks.

Jos (the author)

Dimitri, you're right. This is a somewhat harsh, undocumented and unnecessary restriction. I will update the file shortly.

Dimitri Shvorob

Trying to permute a (20x1) vector:
??? Error using ==> permpos
This function is not usefull for more than 14 entries
Why impose the limit? And why not disclose it upfront?

MATLAB Release
MATLAB 6.5 (R13)


Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today