1
I need to take a vector in vba with any N elements and perform the combination of such elements. The idea is to have a function that takes 2 arguments. The vector with the elements to be combined and the type of combination. 3x3, 4x4, etc.
If this idea is too complicated, because I think it should be used recursion could be an algorithm that performs this task by iteration. In this case 3x3 combined elements.
The vector that was the return of the function will possess the grouped elements and when picking up the grouped elements I will have the desired combination.
Ex: This vector will be inserted into the function, but can be with as many elements as desired. This is just an example.
Array_Items(1)=1
Array_Items(2)=2
Array_Items(3)=3
Array_Items(4)=4
Result of the combination 3x3 in this vector below. If I take this vector of 3 in 3 elements each iteration gives me a combination. That is, the first combination is 1,2,3; the second 1,2,4; the third 1,3,4, etc. until the end. The function must work for any vector size that enters to generate the output 3 to 3. (Of course without breaking the vba limit, but that’s another story). For combination no matter the order. They are unique. The vector that will enter as argument of the function will never have repeated elements. It will have data consistency before.
Comb_array(1)=1
Comb_array(2)=2
Comb_array(3)=3
Comb_array(4)=1
Comb_array(5)=2
Comb_array(6)=4
Comb_array(7)=1
Comb_array(8)=3
Comb_array(9)=4
Comb_array(10)=2
Comb_array(11)=3
Comb_array(12)=4
vba
I imagine two ties inside each other, but I tried to write in a hundred ways and nothing.
Date: 15/09/2019 Function that performs all 3x3 vector items combinators Not implemented
Public Function aMake_Comb_x3(Array_Items() As String) As String()
Dim Index1 As Integer
Dim Index2 As Integer
Dim index3 As Integer
Dim Index4 As Integer
Dim Comb_Array() As Integer
Dim Array_Aux1() As Integer
Dim Cont As Integer
Dim Comb_Type As Integer
Dim X As Integer
Comb_Type = 3
Index1 = 1
Index2 = 2
index3 = 3
Index4 = 1
X = 2
'Redimensiona o vetor para a qtd. max. de combinacoes.
'A posicao 0 sera Ignorada, para facilitar os calculos e loops no preenchimento
ReDim Comb_Array((( WorksheetFunction. combin(UBound(Array_Items) + 1, Comb_Type)) * Comb_Type))
'Redimensiona os vetores para ignorar a posicao "0"
ReDim Array_Aux1(UBound(Array_Items) + 1)
'Preenche o vetor auxiliar
For Cont = 1 To UBound(Array_Items) + 1
Array_Aux1(Cont) = Array_Items(Cont - 1)
Next Cont
For Index4 = 1 To UBound(Comb_Array)
Comb_Array(Index4) = Array_Aux1(Index1)
Index4 = Index4 + 1
For Index2 = X To UBound(Array_Aux1)
Comb_Array(Index4) = Array_Aux1(Index2)
If Index2 Mod Comb_Type = 0 Then
Index2 = X
Exit For
End If
Index4 = Index4 + 1
'If Index4 Mod Comb_Type = 0 Then
'Index2 = Index2 * 2
'End If
Next Index2
Next Index4
End Function
If anyone has an idea or link with such an algorithm would be grateful. I spent a lot of time trying and nothing.
Thanks for your help.
It was not possible to see how it arrived from the input to the result. What is this combination that you apply ?
– Isac
Good evening Isac. The idea is through the input "Array_items" to reach the mentioned result "Comb_array". With this vector grouped where it has the elements combined 3 to 3 of all forms without repetition I get to what I need. The Array Array_items is any vector with several numerical elements. The idea is to combine the numerical content and group in the comb_Array vector. I don’t know if it was clear? Any questions I rephrase my question. Thank you for your attention. Thank you!
– rangelssilva
I forgot to mention. The combination is the "classical" simple combination of mathematics. C n,r. That is a group of elements (Vector) taken three to Three. Grateful for the help.!
– rangelssilva
@rangelssilva Could [Edit] the question with this information? And I don’t know if I understand this correctly, but with a 3x3 combination, you want a combination of three elements, all the possibilities of combination, no matter what the order, so you can’t have duplicates? For, if for example an array is inserted [1,1,2,3], would the result have duplicated 1 or not? And queue an array? If yes, instead of queue, it would not be better a multidimensional matrix, or an array of arrays?
– danieltakeshi
Dear Daniel, good morning. I understood your question. The vector that will enter as an argument in the function will have the previously verified data consistency. Then there is no need to check for duplicate elements. The elements in it will be unique. And the result will be the unique combinations. (Taken 3 to 3). I think it has now become clearer. I am grateful for the help.
– rangelssilva