File Exchange

image thumbnail

NEXTPERMPOS

version 1.0 (2.38 KB) by

the next combination of values in specific positions (extension of PERMPOS)

0 Downloads

Updated

View License

This function is an extension of PERMPOS, for large cases.
A = PERMPOS(M,N) returns a matrix A in which each row contains a unique
permutation of M trues and (N-M) falses over N columns.
When the first input to PERMPOS is a vector V with M elements, each row
of A contains the M values in order, but uniquely distributed over the N
columns of A. See PERMPOS for examples.

The number of rows increases quite rapidly with increasing M and N, in
which case the use of NEXTPERMPOS may be used to avoid memory issues.

CN = NEXTPERMPOS(C) returns a single permutation permutation. The input C
is a vector with N elements, having M non-zero (or true) elements. CN
contains the next permutation of these M values over N positions.
CN has the same size as C.

Examples:
nextpermpos([1 0 0 1 0]) % -> [1 0 0 0 1]
nextpermpos([1 0 1 1 1 0 1]) % -> [1 0 1 1 0 1 1]
nextpermpos([99 0 23]) % -> [0 99 23]
isequal(nextpermpos([1 zeros(1,999) 2 0]), [1 zeros(1,1000) 2])

A = permpos(3,6)
for k = 1:size(A,1)-1
C1 = A(k,:) ;
CN = nextpermpos(C1) ;
if ~isequal(A(k+1,:),CN), disp('This should not happen.') ; end
disp(CN) ;
end

[C2, TF] = NEXTPERMPOS(C) returns a flag that is true if C2 is really the
next permutation. If C is the last possible permutation, C2 will be the
first permutation. Examples:
[c2, tf] = nextpermpos ([0 1 0]) % c2 = [0 0 1], tf = true
[c3, tf] = nextpermpos (c2) % c3 = [1 0 0], tf = false

See also permpos (matlab File Exchange), nchoosek

Comments and Ratings (0)

MATLAB Release
MATLAB 7.13 (R2011b)
Acknowledgements

Inspired by: PERMPOS

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

» Watch video