Solved by JuanFMontesinos in post #2 
              
              
                I don’t really know the formula but it basically works the following way: 
Assume that the number of elements of A and B are the same. 
so what id does is iterating through the dimensions of A starting by the outer one and filling with that B 
B[0]=A[0,0,0,0] 
B[1]=A[0,0,0,1] 
B[nk-1]=A[0,0,0,nk-1] 
…