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
permutation of M trues and (N-M) falses.
W = permpos(V, N) where V is an array with M elements a matrix W
where each row a contains the values of V in preserved order, but
uniquely permuted at the N columns. The remaining positions are set to
NaN. W = permpos(V, N, F) uses the value F rather than NaN.
[W, B] = permpos(V, ...) also returns the logical matrix B, which is
true for the positions that are filled by elements of V. Note that B
equals "permpos(numel(V),N)".
V can be a numerical array, a cell array of chars or a string
array. F (optional) should be of the same class as V.
Examples:
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(1:2:3, 4) % ->
% -> [ 1 2 3 NaN
% 1 2 NaN 3
% 1 NaN 3 3
% NaN 1 2 3 ]
permpos({'a','b'}, 3, 'XX') % filler
% -> { 'a' 'b' 'XX' ; 'a' 'XX' 'b' ; 'XX' 'a' 'b'}
See also nchoosek, perms, randperm, true, false
On the file Exchange:
permn, PERMNK, permsk, allcomb, ballatsq, nones, nextpermpos
Cite As
Jos (10584) (2021). PERMPOS (https://www.mathworks.com/matlabcentral/fileexchange/11216-permpos), MATLAB Central File Exchange.
Retrieved March 17, 2021.
Comments and Ratings (14)
Nice code! Thanks
26 Jun 2014
Absolutely brilliant piece of code
24 Jul 2012
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?
28 May 2012
Very useful, thanks.
19 Feb 2009
Exactly what is needed when finding syndromes for the corresponding bit error patterns when making a decoder for block codes
27 Oct 2008
Amanda, could you contact me directly? See any of my recent submissions for an address. Jos
27 Oct 2008
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
15 Apr 2008
This code is so good.
Berta Neto
28 Mar 2008
It does exactly what I was needing.
Thanks
brad mcevoy
14 Jun 2007
does exactly what i'm looking for. good job and thanks.
Jos (the author)
5 Mar 2007
Dimitri, you're right. This is a somewhat harsh, undocumented and unnecessary restriction. I will update the file shortly.
Dimitri Shvorob
4 Mar 2007
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?