Vectors of all Permutation

ThomasSchmidt

New Member
Joined
Mar 3, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Dear all,

i'm new to this forum, please excuse if i haven't figured out all the necessary details how to post a proper question including tables etc...

I am looking for a way in VBA to get a tabe populated with all conceivable permutations of a binary variable (think of an indicator or something like that). My starting tables look more or less like this:

T____P_1:_____P_2:_____P_3:_____P_4:_____P_5:_____P_6:_____P_7:_____P_8:
1
2
3
...
...

Say my vector-length is t (here T=3), then i have 2^3 =8 vectors containing all possible combinations. What i'm looking for is an algorithm that produces something like this below:

T____P_1:_____P_2:_____P_3:_____P_4:_____P_5:_____P_6:_____P_7:_____P_8:
1____|1|_______|1|_______|1|_______|1|_______|0|_______|0|_______|0|_______|0|
2____|1|_______|0|_______|1|_______|0|_______|1|_______|0|_______|1|_______|0|
3____|1|_______|1|_______|0|_______|0|_______|1|_______|1|_______|0|_______|0|

Needless to say that i have 1200 tables with various T (each table has a specific T-length) but across those tabes T can vary (thus a nested For..loop doesn't work for me i'm afraid) for which i have to perform this on a daily basis, so yes: VBA is obviously the way to do it. I have a working knowledge of VBA (basics) but somehow i'm too blind to see the obvious logic how to populate those tables...

Thank you so much for any help or tips!!

Thomas
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Cell Formulas
RangeFormula
A2:H4A2=IFERROR(MID(DEC2BIN((COLUMN()-COLUMN($A$1)),$A$1),ROW()-ROW($A$1),1),"")
A7:P10A7=IFERROR(MID(DEC2BIN((COLUMN()-COLUMN($A$6)),$A$6),ROW()-ROW($A$6),1),"")
A13:AF17A13=IFERROR(MID(DEC2BIN((COLUMN()-COLUMN($A$12)),$A$12),ROW()-ROW($A$12),1),"")
 
Upvote 0
A way using VBA.

New__Document (60).xlsx
ABCDEFGH
100000000
200000001
300000010
400000011
500000100
600000101
700000110
800000111
900001000
1000001001
1100001010
1200001011
1300001100
1400001101
1500001110
1600001111
1700010000
1800010001
1900010010
2000010011
2100010100
2200010101
2300010110
2400010111
2500011000
2600011001
2700011010
2800011011
2900011100
3000011101
3100011110
3200011111
3300100000
3400100001
3500100010
3600100011
3700100100
3800100101
3900100110
4000100111
4100101000
4200101001
4300101010
4400101011
4500101100
4600101101
4700101110
4800101111
4900110000
5000110001
5100110010
5200110011
5300110100
5400110101
5500110110
5600110111
5700111000
5800111001
5900111010
6000111011
6100111100
6200111101
6300111110
6400111111
6501000000
6601000001
6701000010
6801000011
6901000100
7001000101
7101000110
7201000111
7301001000
7401001001
7501001010
7601001011
7701001100
7801001101
7901001110
8001001111
8101010000
8201010001
8301010010
8401010011
8501010100
8601010101
8701010110
8801010111
8901011000
9001011001
9101011010
9201011011
9301011100
9401011101
9501011110
9601011111
9701100000
9801100001
9901100010
10001100011
10101100100
10201100101
10301100110
10401100111
10501101000
10601101001
10701101010
10801101011
10901101100
11001101101
11101101110
11201101111
11301110000
11401110001
11501110010
11601110011
11701110100
11801110101
11901110110
12001110111
12101111000
12201111001
12301111010
12401111011
12501111100
12601111101
12701111110
12801111111
12910000000
13010000001
13110000010
13210000011
13310000100
13410000101
13510000110
13610000111
13710001000
13810001001
13910001010
14010001011
14110001100
14210001101
14310001110
14410001111
14510010000
14610010001
14710010010
14810010011
14910010100
15010010101
15110010110
15210010111
15310011000
15410011001
15510011010
15610011011
15710011100
15810011101
15910011110
16010011111
16110100000
16210100001
16310100010
16410100011
16510100100
16610100101
16710100110
16810100111
16910101000
17010101001
17110101010
17210101011
17310101100
17410101101
17510101110
17610101111
17710110000
17810110001
17910110010
18010110011
18110110100
18210110101
18310110110
18410110111
18510111000
18610111001
18710111010
18810111011
18910111100
19010111101
19110111110
19210111111
19311000000
19411000001
19511000010
19611000011
19711000100
19811000101
19911000110
20011000111
20111001000
20211001001
20311001010
20411001011
20511001100
20611001101
20711001110
20811001111
20911010000
21011010001
21111010010
21211010011
21311010100
21411010101
21511010110
21611010111
21711011000
21811011001
21911011010
22011011011
22111011100
22211011101
22311011110
22411011111
22511100000
22611100001
22711100010
22811100011
22911100100
23011100101
23111100110
23211100111
23311101000
23411101001
23511101010
23611101011
23711101100
23811101101
23911101110
24011101111
24111110000
24211110001
24311110010
24411110011
24511110100
24611110101
24711110110
24811110111
24911111000
25011111001
25111111010
25211111011
25311111100
25411111101
25511111110
25611111111
Sheet2


VBA Code:
Sub MP()
Dim vLEN As Integer:    vLEN = 3
Dim POW As Integer:     POW = 2 ^ vLEN
Dim AR() As Variant:    AR = Evaluate("TRANSPOSE(ROW(1:" & POW & ")/ROW(1:" & POW & "))")
Dim TMP As Variant:     ReDim TMP(1 To POW)
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")

Perm AR, TMP, 1, AL

With Range("A1").Resize(AL.Count, 1)
    .Value = Application.Transpose(AL.toArray)
    .TextToColumns DataType:=xlDelimited, Space:=True
End With

End Sub

Sub Perm(Org As Variant, TMP As Variant, lInd As Long, AL As Object)
Dim Total As Double

For i = 0 To Org(lInd)
    TMP(lInd) = i
    If lInd = UBound(Org) Then
        AL.Add Join(TMP)
    Else
        Perm Org, TMP, lInd + 1, AL
    End If
Next i
End Sub
 
Upvote 0
Solution
Never mind. Mine does all of them. I misunderstood the OP.
 
Upvote 0
Hi Irobot314, thanks a lot!

I changed your suggested code a bit such i have it in the form that i need. I also like your idea to put the vectors in AL. I guess taging a number on it and store it as a string saves space - in particular if T grows pretty large such that excel cannot handle it anymore...

Thanks also to JGordon11- your trick helps to validate the results:)

Thank you both again for your help!
Thomas
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top