Thread Subject: Array generation question

Subject: Array generation question

From: Stephan Schmidt

Date: 23 Sep, 2010 08:08:08

Message: 1 of 3

Hello, I would like to efficiently do the following:

Input:
N = array size
k = maximum array element size

Output:
All arrays of size n with elements in the range 1..k

Example:
N = 3
k = 3
Output
(1,1,1) (1,1,2)(1,1,3)(1,2,1),(1,2,2),(1,2,3),(1,3,2),(1,3,3),(3,1,1),(3,1,2),(3,1,3),(3,1,2),(3,1,3),(3,2,1),(3,2,2),(3,2,3),(3,3,1),(3,3,2),(3,3,3)

The current implementation repeats the (1,2,..,k) N times and does

for s = 1:N-1
ext = [ext 1:k];
end;
result = unique(nchoosek(extended_input,N),'rows');

This works put is painfully inefficient for larger k due to the creation of the large redundant array ext and then creating its permutations via nchoosek .
Can I do this with native Matlab functions or do I need to write my own creation routine?

Thanks,
Stephan

Subject: Array generation question

From: Oleg Komarov

Date: 23 Sep, 2010 08:38:04

Message: 2 of 3

"Stephan Schmidt" <vomfeinsten@gmx.de> wrote in message <i7f1t8$599$1@fred.mathworks.com>...
> Hello, I would like to efficiently do the following:
>
> Input:
> N = array size
> k = maximum array element size
>
> Output:
> All arrays of size n with elements in the range 1..k
>
> Example:
> N = 3
> k = 3
> Output
> (1,1,1) (1,1,2)(1,1,3)(1,2,1),(1,2,2),(1,2,3),(1,3,2),(1,3,3),(3,1,1),(3,1,2),(3,1,3),(3,1,2),(3,1,3),(3,2,1),(3,2,2),(3,2,3),(3,3,1),(3,3,2),(3,3,3)
>
> The current implementation repeats the (1,2,..,k) N times and does
>
> for s = 1:N-1
> ext = [ext 1:k];
> end;
> result = unique(nchoosek(extended_input,N),'rows');
>
> This works put is painfully inefficient for larger k due to the creation of the large redundant array ext and then creating its permutations via nchoosek .
> Can I do this with native Matlab functions or do I need to write my own creation routine?
>
> Thanks,
> Stephan

Give a look at this very useful submission:
http://www.mathworks.com/matlabcentral/fileexchange/24325-combinator-combinations-and-permutations

If you want to speed up the code you may use the VChoose...MEX submissions. You will find the links on the same page of combinator.

Oleg

Subject: Answered: Array generation question

From: Stephan Schmidt

Date: 23 Sep, 2010 10:59:07

Message: 3 of 3

Thanks a lot Oleg, that's exactly what I was looking for!

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i7f3lc$qij$1@fred.mathworks.com>...
> Give a look at this very useful submission:
> http://www.mathworks.com/matlabcentral/fileexchange/24325-combinator-combinations-and-permutations
>
> If you want to speed up the code you may use the VChoose...MEX submissions. You will find the links on the same page of combinator.
>
> Oleg

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com