File Exchange

image thumbnail

N_PERMUTE_K

version 1.4 (2.52 KB) by

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

4.92308
14 Ratings

19 Downloads

Updated

View License

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

Comments and Ratings (29)

Andrew Burns

This is an exercise of combinations rather than permutations (the order of the 10 stocks doesn't matter). It also is without replacement (i.e., if one of the 13 stocks is IBM, I can only have IBM in the portfolio once).

I miscalculated before - the total combinations is: 286 (not 120) - i.e., choosing combinations of 10 stocks from the pool of 13.

Stephen Cobeldick

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

Clayton

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
your options.

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

Anyway, this code is great!!

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

tommaso

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

tommaso

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

tommaso

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

rahman

rahman (view profile)

Excellent

Works great, thanks

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!

Loginatorist

Loginatorist (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.

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?

GERMÁN

HOW TO CALCULATE THIS FUNCTION?

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

Mark

Mark (view profile)

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

Loginatorist

Loginatorist (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.

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 ???

Syed Galib

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

Gareth

Gareth (view profile)

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

Loginatorist

Loginatorist (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.

Dear Sir Matt,
What's nargin? Thanks

Loginatorist

Loginatorist (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.

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

Husam Aldahiyat

Very helpful.

V. M.

Fast and a good description of what it does.

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.

Jos vdG

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

Jos vdG

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

matt fig (author)

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

Updates

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.

1.2

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

1.1

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

Added keyword.

Added a picture of output.

Faster, due to remedying obvious omission of preallocation!

Error in help.

MATLAB Release
MATLAB 7.4 (R2007a)
Acknowledgements

Inspired by: permn(V, N, K)

Inspired: VChooseKRO, n permute k

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

» Watch video