
View Message Thread (29 replies)
| Combinations |
 |
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

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

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

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

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

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

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

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

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)...
--

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)...
--

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