### Aptech Systems, Inc. Worldwide Headquarters

Address:

Aptech Systems, Inc.

2350 East Germann Road, Suite #21

Chandler, AZ 85286Phone: 360.886.7100

FAX: 360.886.8922**Ready to Get Started?**### Request Quote & Product Information

### Industry Solutions

### Products

### Resources

### Support

### Training & Events

Want more guidance while learning about the full functionality of GAUSS and its capabilities? Get in touch for in-person training or browse additional references below.

### Tutorials

Step-by-step, informative lessons for those who want to dive into GAUSS and achieve their goals, fast.

### Have a Specific Question?

### Q&A: Register and Login

### Support Plans

Premier Support and Platinum Premier Support are annually renewable membership programs that provide you with important benefits including technical support, product maintenance, and substantial cost-saving features for your GAUSS System or the GAUSS Engine.

### User Forums

Join our community to see why our users are considered some of the most active and helpful in the industry!

### Where to Buy

Available across the globe, you can have access to GAUSS no matter where you are.

### Recent Tags

applications character vectors CML CMLMT Constrained Optimization datasets dlibrary dllcall econometrics error error codes error handling errors Excel file i/o floating network GAUSS Engine GAUSS Light graphics GUI hotkeys installation Java API license licensing linux loading data loops matrices matrix manipulation Maxlik MaxLikMT Memory multidimensional array optimization Optmum output panel data PQG graphics procs random numbers strings structures threading writing data### Recent Questions

### Features

### Time Series 2.0 MT

### Industry Solutions

### Find out more now

### Time Series MT 2.1

### Find out more now

# Resources

# permutation

Hi,

is there a command to create a matrix which is all possible permutation of k elements of vector X. Basically, I am looking for an GAUSS equivalence for "perms" and "unique" commands of Matlab.

Thanks in advance!

Cheers

## 3 Answers

Regarding <tt>unique</tt>, GAUSS has a function called <tt>unique</tt> that returns a sorted vector with duplicates removed.

Thanks for this. But I guess perms has no equivalence.

There are currently procedures in the Time Series MT that list permutations and combinations, with and without replacement, given the number of objects and the number of "choice". However, none of these procedures produce the possible permutations given a vector of values.

I believe the three procedures below should allow you to create a matrix of all possible permutation of k elements of vector X. The first of these procedure most directly answers your question and will produce all possible permutations without allowing for replacement of values:

/* **> permVec ** ** Purpose: Calculates the number of permutations without replacement ** ** Format: perms = permVec(x) ** ** Input: x - Vector n x 1, vector for permutation ** ** ** Output: perms - Matrix, list of all possible permutations of the vector x ** without replacement. */ proc (1) =permVec(x);local n,k,x2,x3,tmp,i,e; n = cols(X); k = cols(X); x2 = permReplaceVec(x); //Delete rows with duplicate value x3 = uniqMat(x2); retp(x3); endp;

This procedure calls the two procedures other procedures, permReplaceVec and uniqMat. The first of these, perReplaceVec lists all possible permutations of a vector X allowing for replacement of values:

/* **> permReplaceVec ** ** Purpose: Calculates the number of permutations without replacement ** ** Format: perms = permReplaceVec(x) ** ** Input: x - Vector n x 1, vector for permutation ** ** ** Output: perms - Matrix, list of all possible permutations of the vector x ** with replacement. */ proc (1) =permReplaceVec(x); local nstates, base_idx, c, r, out, base, numiters, numset, k; nstates = rows(x); k = rows(x); c = k; r = nstates^k; out = ones(r, c); base = reshape(seqa(1, 1, nstates), r, 1); for i(1, c-1, 1); numset = nstates^(k-i); base_idx = 1; for j(1, r, numset); out[j:(j+numset-1), i] = base[base_idx].* ones(numset,1); base_idx = base_idx + 1; endfor; endfor; //avoid last iteration of for loop for speed up out[., c] = base; //Matrix Replace for i(1,nstates,1); out = missrv(miss(out,i),x[i]); endfor; retp(out); endp;

The second procedure, uniqMat, deletes all rows from a matrix which include repeated values:

/* **> uniqMat ** ** Purpose: Deletes rows of x which include repeated values ** ** Format: newX = uniqMat(x) ** ** Input: x - Matrix n x k, vector for permutation ** ** ** Output: newX - Matrix, includes only rows of x. */ proc (1) =uniqMat(x2); local tmp, new_X, x3,j; new_X = zeros(rows(X2),1); for i(1,rows(X2),1); tmp = unique(X2[i,.],1); if rows(tmp)!= cols(x2[i,.]); new_X[i,.]=i; endif; endfor; new_X = packr(miss(new_X,0)); x3 = delrows(x2,new_X); retp(x3); endp;

Try the following code as an **example:**

x = rndn(4,1); x; x2 = permReplaceVec(x); x2; x3 = permVec(x); x3;