50,236 Members
2 added today
248,628 Resources
73 added today

All Devdex   All XMLdex   Current Category
XMLdex > Forums & Newsgroups > Newsgroups > microsoft.public.dotnet.xml Add this category to My Favorites

View Message Thread  (29 replies)

Results 1 - 10 of 30 Next Page »

Combinations Add this thread to My Favorites
From: Shell
Date Posted: 6/2/2010 9:29:00 AM

In VB6 I have an application where I have 7 characters (numbers and/or
letters).  I need to generate a list (probably in a database) on all possible
combinations of 3, 4, 5, 6, and 7 characters.

I keep getting mixed up, trying recursive functions, which I hate. Does
anyone have a simpler way of doing this?

Thanks
--
Shell

Combinations
From: Shell
Date Posted: 6/2/2010 9:30:00 AM

In VB6 I have an application where I have 7 characters (numbers and/or
letters).  I need to generate a list (probably in a database) on all possible
combinations of 3, 4, 5, 6, and 7 characters.

I keep getting mixed up, trying recursive functions, which I hate. Does
anyone have a simpler way of doing this?

Thanks
--
Shell

Combinations
From: Shell
Date Posted: 6/2/2010 9:32:00 AM

In VB6 I have an application where I have 7 characters (numbers and/or
letters).  I need to generate a list (probably in a database) on all possible
combinations of 3, 4, 5, 6, and 7 characters.

I keep getting mixed up, trying recursive functions, which I hate. Does
anyone have a simpler way of doing this?

Thanks
--
Shell

Combinations
From: Shell
Date Posted: 6/2/2010 9:33:00 AM

In VB6 I have an application where I have 7 characters (numbers and/or
letters).  I need to generate a list (probably in a database) on all possible
combinations of 3, 4, 5, 6, and 7 characters.

I keep getting mixed up, trying recursive functions, which I hate. Does
anyone have a simpler way of doing this?

Thanks
--
Shell

Re: Combinations
From: dpb
Date Posted: 6/2/2010 9:56:00 AM

Shell wrote:
> In VB6 I have an application where I have 7 characters (numbers and/or
> letters).  I need to generate a list (probably in a database) on all possible
> combinations of 3, 4, 5, 6, and 7 characters.
>
> I keep getting mixed up, trying recursive functions, which I hate. Does
> anyone have a simpler way of doing this?

I got the last one... ;)

How about...

>> nchoosek([1:7],7)
ans =
      1     2     3     4     5     6     7
>> nchoosek([1:7],6)
ans =
      1     2     3     4     5     6
      1     2     3     4     5     7
      1     2     3     4     6     7
      1     2     3     5     6     7
      1     2     4     5     6     7
      1     3     4     5     6     7
      2     3     4     5     6     7
>> nchoosek([1:7],5)
ans =
      1     2     3     4     5
      1     2     3     4     6
      1     2     3     4     7
      1     2     3     5     6
      1     2     3     5     7
      1     2     3     6     7
      1     2     4     5     6
      1     2     4     5     7
      1     2     4     6     7
      1     2     5     6     7
      1     3     4     5     6
      1     3     4     5     7
      1     3     4     6     7
      1     3     5     6     7
      1     4     5     6     7
      2     3     4     5     6
      2     3     4     5     7
      2     3     4     6     7
      2     3     5     6     7
      2     4     5     6     7
      3     4     5     6     7
>> nchoosek([1:7],4)
ans =
      1     2     3     4
      1     2     3     5
      1     2     3     6
      1     2     3     7
      1     2     4     5
      1     2     4     6
      1     2     4     7
      1     2     5     6
      1     2     5     7
      1     2     6     7
      1     3     4     5
      1     3     4     6
      1     3     4     7
      1     3     5     6
      1     3     5     7
      1     3     6     7
      1     4     5     6
      1     4     5     7
      1     4     6     7
      1     5     6     7
      2     3     4     5
      2     3     4     6
      2     3     4     7
      2     3     5     6
      2     3     5     7
      2     3     6     7
      2     4     5     6
      2     4     5     7
      2     4     6     7
      2     5     6     7
      3     4     5     6
      3     4     5     7
      3     4     6     7
      3     5     6     7
      4     5     6     7
>> nchoosek([1:7],3)
ans =
      1     2     3
      1     2     4
      1     2     5
      1     2     6
      1     2     7
      1     3     4
      1     3     5
      1     3     6
      1     3     7
      1     4     5
      1     4     6
      1     4     7
      1     5     6
      1     5     7
      1     6     7
      2     3     4
      2     3     5
      2     3     6
      2     3     7
      2     4     5
      2     4     6
      2     4     7
      2     5     6
      2     5     7
      2     6     7
      3     4     5
      3     4     6
      3     4     7
      3     5     6
      3     5     7
      3     6     7
      4     5     6
      4     5     7
      4     6     7
      5     6     7
>>

Ain't Matlab useful sometimes???? :)

--

Re: Combinations
From: dpb
Date Posted: 6/2/2010 9:57:00 AM

Shell wrote:
> In VB6 I have an application where I have 7 characters (numbers and/or
> letters).  I need to generate a list (probably in a database) on all possible
> combinations of 3, 4, 5, 6, and 7 characters.
>
> I keep getting mixed up, trying recursive functions, which I hate. Does
> anyone have a simpler way of doing this?

I got the last one... ;)

How about...

>> nchoosek([1:7],7)
ans =
      1     2     3     4     5     6     7
>> nchoosek([1:7],6)
ans =
      1     2     3     4     5     6
      1     2     3     4     5     7
      1     2     3     4     6     7
      1     2     3     5     6     7
      1     2     4     5     6     7
      1     3     4     5     6     7
      2     3     4     5     6     7
>> nchoosek([1:7],5)
ans =
      1     2     3     4     5
      1     2     3     4     6
      1     2     3     4     7
      1     2     3     5     6
      1     2     3     5     7
      1     2     3     6     7
      1     2     4     5     6
      1     2     4     5     7
      1     2     4     6     7
      1     2     5     6     7
      1     3     4     5     6
      1     3     4     5     7
      1     3     4     6     7
      1     3     5     6     7
      1     4     5     6     7
      2     3     4     5     6
      2     3     4     5     7
      2     3     4     6     7
      2     3     5     6     7
      2     4     5     6     7
      3     4     5     6     7
>> nchoosek([1:7],4)
ans =
      1     2     3     4
      1     2     3     5
      1     2     3     6
      1     2     3     7
      1     2     4     5
      1     2     4     6
      1     2     4     7
      1     2     5     6
      1     2     5     7
      1     2     6     7
      1     3     4     5
      1     3     4     6
      1     3     4     7
      1     3     5     6
      1     3     5     7
      1     3     6     7
      1     4     5     6
      1     4     5     7
      1     4     6     7
      1     5     6     7
      2     3     4     5
      2     3     4     6
      2     3     4     7
      2     3     5     6
      2     3     5     7
      2     3     6     7
      2     4     5     6
      2     4     5     7
      2     4     6     7
      2     5     6     7
      3     4     5     6
      3     4     5     7
      3     4     6     7
      3     5     6     7
      4     5     6     7
>> nchoosek([1:7],3)
ans =
      1     2     3
      1     2     4
      1     2     5
      1     2     6
      1     2     7
      1     3     4
      1     3     5
      1     3     6
      1     3     7
      1     4     5
      1     4     6
      1     4     7
      1     5     6
      1     5     7
      1     6     7
      2     3     4
      2     3     5
      2     3     6
      2     3     7
      2     4     5
      2     4     6
      2     4     7
      2     5     6
      2     5     7
      2     6     7
      3     4     5
      3     4     6
      3     4     7
      3     5     6
      3     5     7
      3     6     7
      4     5     6
      4     5     7
      4     6     7
      5     6     7
>>

Ain't Matlab useful sometimes???? :)

--

Re: Combinations
From: dpb
Date Posted: 6/2/2010 9:58:00 AM

Shell wrote:
> In VB6 I have an application where I have 7 characters (numbers and/or
> letters).  I need to generate a list (probably in a database) on all possible
> combinations of 3, 4, 5, 6, and 7 characters.
>
> I keep getting mixed up, trying recursive functions, which I hate. Does
> anyone have a simpler way of doing this?

I got the last one... ;)

How about...

>> nchoosek([1:7],7)
ans =
      1     2     3     4     5     6     7
>> nchoosek([1:7],6)
ans =
      1     2     3     4     5     6
      1     2     3     4     5     7
      1     2     3     4     6     7
      1     2     3     5     6     7
      1     2     4     5     6     7
      1     3     4     5     6     7
      2     3     4     5     6     7
>> nchoosek([1:7],5)
ans =
      1     2     3     4     5
      1     2     3     4     6
      1     2     3     4     7
      1     2     3     5     6
      1     2     3     5     7
      1     2     3     6     7
      1     2     4     5     6
      1     2     4     5     7
      1     2     4     6     7
      1     2     5     6     7
      1     3     4     5     6
      1     3     4     5     7
      1     3     4     6     7
      1     3     5     6     7
      1     4     5     6     7
      2     3     4     5     6
      2     3     4     5     7
      2     3     4     6     7
      2     3     5     6     7
      2     4     5     6     7
      3     4     5     6     7
>> nchoosek([1:7],4)
ans =
      1     2     3     4
      1     2     3     5
      1     2     3     6
      1     2     3     7
      1     2     4     5
      1     2     4     6
      1     2     4     7
      1     2     5     6
      1     2     5     7
      1     2     6     7
      1     3     4     5
      1     3     4     6
      1     3     4     7
      1     3     5     6
      1     3     5     7
      1     3     6     7
      1     4     5     6
      1     4     5     7
      1     4     6     7
      1     5     6     7
      2     3     4     5
      2     3     4     6
      2     3     4     7
      2     3     5     6
      2     3     5     7
      2     3     6     7
      2     4     5     6
      2     4     5     7
      2     4     6     7
      2     5     6     7
      3     4     5     6
      3     4     5     7
      3     4     6     7
      3     5     6     7
      4     5     6     7
>> nchoosek([1:7],3)
ans =
      1     2     3
      1     2     4
      1     2     5
      1     2     6
      1     2     7
      1     3     4
      1     3     5
      1     3     6
      1     3     7
      1     4     5
      1     4     6
      1     4     7
      1     5     6
      1     5     7
      1     6     7
      2     3     4
      2     3     5
      2     3     6
      2     3     7
      2     4     5
      2     4     6
      2     4     7
      2     5     6
      2     5     7
      2     6     7
      3     4     5
      3     4     6
      3     4     7
      3     5     6
      3     5     7
      3     6     7
      4     5     6
      4     5     7
      4     6     7
      5     6     7
>>

Ain't Matlab useful sometimes???? :)

--

Re: Combinations
From: dpb
Date Posted: 6/2/2010 9:59:00 AM

Shell wrote:
> In VB6 I have an application where I have 7 characters (numbers and/or
> letters).  I need to generate a list (probably in a database) on all possible
> combinations of 3, 4, 5, 6, and 7 characters.
>
> I keep getting mixed up, trying recursive functions, which I hate. Does
> anyone have a simpler way of doing this?

I got the last one... ;)

How about...

>> nchoosek([1:7],7)
ans =
      1     2     3     4     5     6     7
>> nchoosek([1:7],6)
ans =
      1     2     3     4     5     6
      1     2     3     4     5     7
      1     2     3     4     6     7
      1     2     3     5     6     7
      1     2     4     5     6     7
      1     3     4     5     6     7
      2     3     4     5     6     7
>> nchoosek([1:7],5)
ans =
      1     2     3     4     5
      1     2     3     4     6
      1     2     3     4     7
      1     2     3     5     6
      1     2     3     5     7
      1     2     3     6     7
      1     2     4     5     6
      1     2     4     5     7
      1     2     4     6     7
      1     2     5     6     7
      1     3     4     5     6
      1     3     4     5     7
      1     3     4     6     7
      1     3     5     6     7
      1     4     5     6     7
      2     3     4     5     6
      2     3     4     5     7
      2     3     4     6     7
      2     3     5     6     7
      2     4     5     6     7
      3     4     5     6     7
>> nchoosek([1:7],4)
ans =
      1     2     3     4
      1     2     3     5
      1     2     3     6
      1     2     3     7
      1     2     4     5
      1     2     4     6
      1     2     4     7
      1     2     5     6
      1     2     5     7
      1     2     6     7
      1     3     4     5
      1     3     4     6
      1     3     4     7
      1     3     5     6
      1     3     5     7
      1     3     6     7
      1     4     5     6
      1     4     5     7
      1     4     6     7
      1     5     6     7
      2     3     4     5
      2     3     4     6
      2     3     4     7
      2     3     5     6
      2     3     5     7
      2     3     6     7
      2     4     5     6
      2     4     5     7
      2     4     6     7
      2     5     6     7
      3     4     5     6
      3     4     5     7
      3     4     6     7
      3     5     6     7
      4     5     6     7
>> nchoosek([1:7],3)
ans =
      1     2     3
      1     2     4
      1     2     5
      1     2     6
      1     2     7
      1     3     4
      1     3     5
      1     3     6
      1     3     7
      1     4     5
      1     4     6
      1     4     7
      1     5     6
      1     5     7
      1     6     7
      2     3     4
      2     3     5
      2     3     6
      2     3     7
      2     4     5
      2     4     6
      2     4     7
      2     5     6
      2     5     7
      2     6     7
      3     4     5
      3     4     6
      3     4     7
      3     5     6
      3     5     7
      3     6     7
      4     5     6
      4     5     7
      4     6     7
      5     6     7
>>

Ain't Matlab useful sometimes???? :)

--

Re: Combinations
From: dpb
Date Posted: 6/2/2010 11:10:00 AM

dpb wrote:
> Shell wrote:
....
>> ...need a list of all possible combinations of 3, 4, 5, 6, and 7
>> characters from seven.
>>
>> I keep getting mixed up, trying recursive functions, which I hate.
>> Does anyone have a simpler way of doing this?
>
> I got the last one... ;)
>
> How about...
>
>  >> nchoosek([1:7],7)
> ans =
>      1     2     3     4     5     6     7
>  >> nchoosek([1:7],6)
> ans =
>      1     2     3     4     5     6
>      1     2     3     4     5     7
....
>      3     5     6     7
>      4     5     6     7
>  >> nchoosek([1:7],3)
> ans =
>      1     2     3
....
>      4     6     7
>      5     6     7
>  >>
>
> Ain't Matlab useful sometimes???? :)
>
> --
Here's a recursive solution, in Matlab.

I've added some comments to the Matlab code to aid in converting it if
you wish to do the generation on the fly rather than use the results
stored as lookup tables. Assuming you have a string, the above can be
used iteratively as the index vectors into the string to select the
various combinations as desired.

function P = combs(v,m)

In Matlab, the syntax for a function is that the return value is the
variable following the keyword function. "Vector" is synonymous w/ 1D
array and "row/column" have same orientation as does VB (column-major
storage order)

%COMBS All possible combinations.
%   COMBS(V,M) where V is a row vector of length N,
%   creates a matrix with N!/((N-M)! M!) rows and M columns containing
%   all possible combinations of N elements taken M at a time.
%
%   This function is only practical for situations where M is less
%   than about 15.

if nargin~=2, error('Requires 2 input arguments.'); end

v = v(:).'; % Make sure v is a row vector.

n = length(v);  % == UBound-LBound+1
if n == m       % the select 'em all case
   P = v;
elseif m == 1   % the other extreme of only one at a time
                 % return n rows w/ one element each
                 % your case won't ever use this as you said m>2
   P = v.';
else            % all the rest...
   P = [];      % starts w/ empty array...
   if m < n & m > 1  % culls out the possible erroneous input
     for k = 1:n-m+1
       Q = combs(v(k+1:n),m-1);
The [ ] enclose an array expression in Matlab. This will have to be
expanded into a nested loop in VB. The semicolon concatenates the
following expression as the next row in the array P.

Reading the expression [v(ones(size(Q,1),1),k) Q] from inside out,
size(Q,1) is the number of rows in Q this iteration (size of first
dimension of Q), we'll temporarily call it nRows.

Then ones(nRows,1) returns a 1D column array of ones and so
v(ones(nRows,1),k) is v(1,k) for each pass thru the loop. This value is
then concatenated onto the row.

       P = [P; [v(ones(size(Q,1),1),k) Q]];
     end
   end
end


Enjoy!!! :)

I've gotta' run at the moment; if have some time later on maybe I'll see
about translating it (but no promises, have some other commitments)...

--

Re: Combinations
From: dpb
Date Posted: 6/2/2010 11:12:00 AM

dpb wrote:
> Shell wrote:
....
>> ...need a list of all possible combinations of 3, 4, 5, 6, and 7
>> characters from seven.
>>
>> I keep getting mixed up, trying recursive functions, which I hate.
>> Does anyone have a simpler way of doing this?
>
> I got the last one... ;)
>
> How about...
>
>  >> nchoosek([1:7],7)
> ans =
>      1     2     3     4     5     6     7
>  >> nchoosek([1:7],6)
> ans =
>      1     2     3     4     5     6
>      1     2     3     4     5     7
....
>      3     5     6     7
>      4     5     6     7
>  >> nchoosek([1:7],3)
> ans =
>      1     2     3
....
>      4     6     7
>      5     6     7
>  >>
>
> Ain't Matlab useful sometimes???? :)
>
> --
Here's a recursive solution, in Matlab.

I've added some comments to the Matlab code to aid in converting it if
you wish to do the generation on the fly rather than use the results
stored as lookup tables. Assuming you have a string, the above can be
used iteratively as the index vectors into the string to select the
various combinations as desired.

function P = combs(v,m)

In Matlab, the syntax for a function is that the return value is the
variable following the keyword function. "Vector" is synonymous w/ 1D
array and "row/column" have same orientation as does VB (column-major
storage order)

%COMBS All possible combinations.
%   COMBS(V,M) where V is a row vector of length N,
%   creates a matrix with N!/((N-M)! M!) rows and M columns containing
%   all possible combinations of N elements taken M at a time.
%
%   This function is only practical for situations where M is less
%   than about 15.

if nargin~=2, error('Requires 2 input arguments.'); end

v = v(:).'; % Make sure v is a row vector.

n = length(v);  % == UBound-LBound+1
if n == m       % the select 'em all case
   P = v;
elseif m == 1   % the other extreme of only one at a time
                 % return n rows w/ one element each
                 % your case won't ever use this as you said m>2
   P = v.';
else            % all the rest...
   P = [];      % starts w/ empty array...
   if m < n & m > 1  % culls out the possible erroneous input
     for k = 1:n-m+1
       Q = combs(v(k+1:n),m-1);
The [ ] enclose an array expression in Matlab. This will have to be
expanded into a nested loop in VB. The semicolon concatenates the
following expression as the next row in the array P.

Reading the expression [v(ones(size(Q,1),1),k) Q] from inside out,
size(Q,1) is the number of rows in Q this iteration (size of first
dimension of Q), we'll temporarily call it nRows.

Then ones(nRows,1) returns a 1D column array of ones and so
v(ones(nRows,1),k) is v(1,k) for each pass thru the loop. This value is
then concatenated onto the row.

       P = [P; [v(ones(size(Q,1),1),k) Q]];
     end
   end
end


Enjoy!!! :)

I've gotta' run at the moment; if have some time later on maybe I'll see
about translating it (but no promises, have some other commitments)...

--

Results 1 - 10 of 30 Next Page »

 

Would you like to track this thread?

By adding this News Thread to your Favorites Area you can refer to it later with just a click of the mouse. Also you can optionally be notified instantly whenever there are any replies posted to this Thread. To add this Thread to your Favorites Area just click on the red arrow next to the subject of the thread above Add this thread to My Favorites.



ASP.NET Web Hosting
- FREE Setup & Domain
- First month FREE
100% IIS6 / Server 2003

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 

 

 

 

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.