Code covered by the BSD License  

Highlights from
N_PERMUTE_K

4.88889

4.9 | 9 ratings Rate this file 55 Downloads (last 30 days) File Size: 2.51 KB File ID: #11462
image thumbnail

N_PERMUTE_K

by Matt Fig

 

19 Jun 2006 (Updated 11 May 2009)

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

| Watch this File

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

The author wishes to acknowledge the following in the creation of this submission:
COMBN (4.2)
This submission has inspired the following:
VChooseKRO

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (21)
19 Jun 2006 matt fig (author)

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

19 Jun 2006 Jos vdG

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

19 Jun 2006 Jos vdG

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

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.

30 Sep 2008 V. M.

Fast and a good description of what it does.

31 Jan 2009 Husam Aldahiyat

Very helpful.

04 May 2009 Hoda El Mahallawy

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

04 May 2009 Matt Fig

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.

04 May 2009 Hoda El Mahallawy

Dear Sir Matt,
What's nargin? Thanks

04 May 2009 Matt Fig

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

05 May 2009 Gareth

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

25 Jun 2009 Syed Galib

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

13 Jul 2009 Juliette Salexa

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

13 Jul 2009 Matt Fig

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.

25 Sep 2009 Mark

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

07 Oct 2009 GERMÁN

HOW TO CALCULATE THIS FUNCTION?

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

17 Dec 2009 Jan Simon

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?

17 Dec 2009 Matt Fig

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.

01 Jan 2010 Jan Simon

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 Mar 2010 zampala ballarini

Works great, thanks

16 May 2011 rahman

Excellent

Please login to add a comment or rating.
Updates
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

Added keyword.

13 Oct 2008

Uploaded the wrong file.

07 May 2009

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

11 May 2009

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

Tag Activity for this File
Tag Applied By Date/Time
permutations Matt Fig 22 Oct 2008 08:29:56
combinations Matt Fig 22 Oct 2008 08:29:56
multisets Matt Fig 22 Oct 2008 08:29:56
probability Matt Fig 22 Oct 2008 08:29:56
nchoosek Matt Fig 22 Oct 2008 08:29:56
c Matt Fig 22 Oct 2008 08:29:56
sample Matt Fig 22 Oct 2008 08:29:56
c Brandon 08 Apr 2009 04:42:05
choose Matt Fig 08 May 2009 10:09:59
combinatorics Matt Fig 11 May 2009 11:04:15
multisets galib 12 Sep 2010 23:22:32

Contact us at files@mathworks.com