Code covered by the BSD License

### Highlights from N_PERMUTE_K

4.92308
4.9 | 14 ratings Rate this file 67 Downloads (last 30 days) File Size: 2.52 KB File ID: #11462 Version: 1.4

# N_PERMUTE_K

by

### Matt Fig (view profile)

19 Jun 2006 (Updated )

All possible permutations of the elements of set N, taken K at a time, with repetition.

File Information
Description

MAT = NPERMUTEK(N,K) returns all possible permutations of the elements taken from vector N of length K. This type of sampling is an ordered sample with replacement.
This is also known as the permutations with repetition.
MAT has size (length(N)^K)-by-K, where K must be a scalar.

[MAT, IDX] = NPERMUTEK(N,K) also returns IDX such that MAT = N(IDX).

If N is of class single, MAT and IDX will be also.

Please email me about bugs in the code.
Thanks

Acknowledgements

Permn(V, N, K) inspired this file.

This file inspired N Permute K and V Choose Kro.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Comments and Ratings (28)
12 May 2015 Stephen Cobeldick

### Stephen Cobeldick (view profile)

Finally a combinatorics submission that knows the difference between combinations and permutations!

18 Feb 2015 Clayton

### Clayton (view profile)

11 Nov 2014 Grace

### Grace (view profile)

Hi, is this code has any limitation?

I used to find npermutek(2:14,7) and it showed

>> npermutek(2:14,7)
Error using zeros
Out of memory. Type HELP MEMORY for

Error in npermutek (line 87)
Matrix = zeros(L,K,CLS); %
Preallocation.

Anyway, this code is great!!

Comment only
30 Jun 2013 Philip

### Philip (view profile)

Great code! Is it at all feasible for the following ever to work?

nperutek(['A':'Z' '1':'9'],22

My initial though is not a chance in hell! i get an out of memory message when i try it with 10 length arrays.

Thanks

17 Nov 2012 tommaso

### tommaso (view profile)

hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great

17 Nov 2012 tommaso

### tommaso (view profile)

hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great

17 Nov 2012 tommaso

### tommaso (view profile)

hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great

Comment only
16 May 2011 rahman

### rahman (view profile)

Excellent

17 Mar 2010 zampala ballarini

### zampala ballarini (view profile)

Works great, thanks

01 Jan 2010 Jan Simon

### Jan Simon (view profile)

Thanks Matt! Now I got it.
For small arrays, this function is faster than your COMBINATOR.
H1-Line, help, example, contact address, "see also", input checks, extensive comments, works correct and efficiently => nice for teaching and computing!

17 Dec 2009 Matt Fig

### Matt Fig (view profile)

Jan,
With the single class we are ok up to 16,000,000 elements. Even finding the number taken two at a time would lead to an array of size 5.12e+014 elements. I think this is beyond the reasonable, even with 64-bit computing! The other problem is that cumsum isn't supported for the integer classes, at least in 2007b. If you look at my COMBINATOR file, you will see that I have included a MEX which performs cumsum on integer classes.
Thanks.

Comment only
17 Dec 2009 Jan Simon

### Jan Simon (view profile)

Why is the index IDX a SINGLE array, if N is a SINGLE? Isn't this an unnecessary limitation when working with large arrays on a 64-bit Matlab? UINT32 would accept larger arrays with using all 32 bits for adressing. And addressing with UINT32 might be faster than with SINGLEs (depending on Matlab version?!).
--- But of course: The number of permutations will explode at far smaller limits, I know.

Is it helpful to cast CHAR to DOUBLEs temporarily, or would it be better to store them in smaller INT16?

Comment only
07 Oct 2009 GERMÁN

### GERMÁN (view profile)

HOW TO CALCULATE THIS FUNCTION?

MAT = npermutek(['a' 'b' 'c' '!' 'l' 't' 's' '0' '1' 'e' 'o' 'i' ],16)

Comment only
25 Sep 2009 Mark

### Mark (view profile)

totally terrific, works as advertised and saves me a bunch of hassle.

13 Jul 2009 Matt Fig

### Matt Fig (view profile)

Juliette,

You cannot index the output of npermutek(1:5,5) into an array (nuc) of length 4! The index exceeds dimensions because the output of the above call to NPERMUTEK will have many '5's in it. You will get the same error by doing this:

nuc = 'ABCD'
R = nuc(5) % or nuc(6) or nuc(7), etc. nuc only has 4 elements!

This is not an NPERMUTEK problem, it a user misunderstanding the output problem.

Comment only
13 Jul 2009 Juliette Salexa

### Juliette Salexa (view profile)

Does this not work for large arrays ??

>> nuc='ABCD';
>> clear('R');n=1;R=zeros(4^n,n);R=nuc(npermutek(1:n,n));
works.
>> clear('R');n=2;R=zeros(4^n,n);R=nuc(npermutek(1:n,n));
works.
>> clear('R');n=3;R=zeros(4^n,n);R=nuc(npermutek(1:n,n));
works.
>> clear('R');n=4;R=zeros(4^n,n);R=nuc(npermutek(1:n,n));
works.
>> clear('R');n=5;R=zeros(4^n,n);R=nuc(npermutek(1:n,n));
??? Index exceeds matrix dimensions.

------------------
I'm not entirely sure where the index is exceeding matrix dimensions.... if only matlab was more specific with error listings.
------------------
But maybe this is only happening on my machine... can someone try and let me know ???

Comment only
25 Jun 2009 Syed Galib

### Syed Galib (view profile)

It is really a nice thing...it helped me a lot.
Thank you author for this

05 May 2009 Gareth

### Gareth (view profile)

just what I wanted, worked as I expected, great comments. Thanks

04 May 2009 Matt Fig

### Matt Fig (view profile)

nargin is a built-in Matlab function, as is numel. Take these questions to email please! My email address is in the file.

Comment only
04 May 2009 Hoda El Mahallawy

### Hoda El Mahallawy (view profile)

Dear Sir Matt,
What's nargin? Thanks

Comment only
04 May 2009 Matt Fig

### Matt Fig (view profile)

Hoda,

numel is a built-in Matlab function which returns the Number of ELements in the argument. If your version of Matlab is too old to have numel, you may have to write your own. For vector inputs to npermutek, you can also replace numel with length.

Comment only
04 May 2009 Hoda El Mahallawy

### Hoda El Mahallawy (view profile)

what's nume1? that's used for calculating the index. where's the help that you said you missed sth in ? Thanks

31 Jan 2009 Husam Aldahiyat

### Husam Aldahiyat (view profile)

30 Sep 2008 V. M.

Fast and a good description of what it does.

20 Jun 2006 matt fig (author)

I thought that by defining the last column of the Index matrix first, there would be no advantage to pre-allocation. I was wrong, live and learn. File updated.

Comment only
19 Jun 2006 Jos vdG

sorry .. didn't see the acknowledgement ...
Jos

Comment only
19 Jun 2006 Jos vdG

Did you know <combnk> also here on the FEX? (File # 7147).
Looks like that does exactly the same.

Comment only
19 Jun 2006 matt fig (author)

I noticed that I missed a comment line in the help... this is fixed.

Comment only
19 Jun 2006

Error in help.

20 Jun 2006

Faster, due to remedying obvious omission of preallocation!

02 Oct 2008

Added a picture of output.

10 Oct 2008

07 May 2009 1.1

Cut execution time in half for single output calls. Added full support for single class.

11 May 2009 1.2

Fixed typos (Matlab needs spellchecker for guys like me.)

31 Aug 2012 1.4

MathWorks changed the behavior of the DIFF function with character arrays, resulting in an error for this file when called as in the second help example.