PC Review


Reply
Thread Tools Rate Thread

all permutations

 
 
KAH
Guest
Posts: n/a
 
      29th May 2009
Hi. I am trying to generate all possible single arrays which consist of one
element in each row of a double array in VisualBasic in Excel. For example,
if DoubleArray(R,C) is first row 1,2,3 and second row 4,5,6; then I want
NewArray(R) of (1,4), (1,5), (1,6), (2,4), (2,5) and (2,6). I have figured
out how to do this with specific numbers of rows and columns, but I want to
do this with a variable number of rows and columns (input by the user). Here
is what I have:
If Rows = 3 Then
For i = 1 To Columns
For j = 1 To Columns
For k = 1 To Columns
NewArray(1) = DoubleArray(1, i)
NewArray (2) = DoubleArray(2, j)
NewArray (3) = DoubleArray(3, k)
Functions
Next k
Next j
Next i
ElseIf Rows = 4 Then
For i = 1 To Columns

End If
'Functions' are simply what I want to do with the single arrays.
How can I change this so it can work with any number of rows and columns?
Thanks for any help.
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      29th May 2009
Dim NewArray As Variant
NumberofRows = UBound(DoubleArray, 1)
NumberofCols = UBound(DoubleArray, 2)

NewArrayIndex = 1
ReDim NewArray(NumberofRows * NumberofCols)
For RowCount = 1 To NumberofRows
For ColCount = 1 To NumberofCols
NewArray(NewArrayIndex) = _
DoubleArray(RowCount, ColCount)
NewArrayIndex = NewArrayIndex + 1
Next ColCount
Next RowCount

"KAH" wrote:

> Hi. I am trying to generate all possible single arrays which consist of one
> element in each row of a double array in VisualBasic in Excel. For example,
> if DoubleArray(R,C) is first row 1,2,3 and second row 4,5,6; then I want
> NewArray(R) of (1,4), (1,5), (1,6), (2,4), (2,5) and (2,6). I have figured
> out how to do this with specific numbers of rows and columns, but I want to
> do this with a variable number of rows and columns (input by the user). Here
> is what I have:
> If Rows = 3 Then
> For i = 1 To Columns
> For j = 1 To Columns
> For k = 1 To Columns
> NewArray(1) = DoubleArray(1, i)
> NewArray (2) = DoubleArray(2, j)
> NewArray (3) = DoubleArray(3, k)
> Functions
> Next k
> Next j
> Next i
> ElseIf Rows = 4 Then
> For i = 1 To Columns
> …
> End If
> 'Functions' are simply what I want to do with the single arrays.
> How can I change this so it can work with any number of rows and columns?
> Thanks for any help.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
permutations art Microsoft Excel Worksheet Functions 1 8th May 2009 05:07 PM
Permutations Jose Microsoft C# .NET 1 23rd Mar 2006 02:34 PM
permutations =?Utf-8?B?bmV3eW9ya2pveQ==?= Microsoft Excel Misc 3 1st Nov 2005 08:20 PM
Permutations =?Utf-8?B?UmVkQ2hlcXVlcg==?= Microsoft Excel Worksheet Functions 9 26th Sep 2005 03:14 AM
Permutations =?Utf-8?B?UmVkQ2hlcXVlcg==?= Microsoft Excel Worksheet Functions 2 25th Sep 2005 04:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:16 AM.