0

I'm struggling with finding a sorting algorithm that can sort the columns of a multidimensional array. All columns should be sorted individually (ascending) without a link with the other columns. I have tried to use the code proposed in this thread: Sorting a multidimensionnal array in VBA

But all I get is the value from the last entry in the last column.For instance if the first column is (3, 5, 1, 9) and the second column (5, 4, 8, 1) all I get are 1's in the whole array. What I want is (1, 3, 5, 9) in the first column and (1, 4, 5, 8) in the second.

I call the sort algorith with the following command: QuickSortArray Arraytosort, , , 1

to sort the first column. The plan was to later on make a loop to sort all columns.

I have two questions:

What am I doing wrong?

and can I use this code for this problem in the first place? Or does it need to be adjusted?

This is the code so far:

Sub P_Values()

Dim threshold As Long                       'threshold
Dim N As Long                               'number of samples
Dim M As Long                               'number of sims
N = Cells(2, 2)
M = Cells(3, 2)
ReDim Samples(N, M)                         'array to store samples
ReDim CM(M)
Dim teller_N As Long                        'counter sample
Dim teller_M As Long                        'counter sims
Dim teta As Double                          'teta parameter

teta = Cells(3, 6)
threshold = Cells(1, 2)

'generate samples
For teller_M = 1 To M
    For teller_N = 1 To N
        Samples(N, M) = sev_expo(teta, threshold)
        Cells(6 + teller_N, 1 + teller_M).Value = Samples(N, M)
    Next
Next

'sort sims
QuickSortArray Samples(), , , 1

And the code from the function sev_expo:

Function sev_expo(ByVal teta As Double, ByVal threshold As Long) As Long

    Dim U As Double
    U = Rnd

    sev_expo = -teta * Log(1 - U) + threshold

End Function
  • Could you show us the rest of your code? – Tom K. Mar 06 '15 at 16:00
  • Is this intended `Samples(N, M) = sev_expo(teta, threshold)`? I mean, I don't see the value in the array changing while you are looping because teta and threshold doesn't change inside the loop. – shahkalpesh Mar 06 '15 at 16:19
  • Yes, I forgot to mention that sev_expo is a function that generates the numbers from an exponential distribution. I'll add that code too. – David van Zuijlekom Mar 06 '15 at 16:27
  • Doe it mean the function returns different values each time, while the parameters to the function have same values passed to it? How does your samples() array look like after the for loop? – shahkalpesh Mar 06 '15 at 16:33
  • Yes, the function returns random number each time it is accessed. The samples() array contains the random samples after the loop. – David van Zuijlekom Mar 06 '15 at 16:53
  • The numbers are large integer values, like 822,456 and 3,424,967, etc. – David van Zuijlekom Mar 06 '15 at 17:00

1 Answers1

0

If found the eror in my code. I should have stored the samples in samples(teller_N, teller_M) instead of samples(N, M). Really stupid, but it happens when you are working for too long...

So it now works in the way that it sorts my columns but not independently from each other. Is there a simple way to change the code? Or should I use the one column per time version?

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27