I am trying to move data from 2 Double Arrays to 2 different Double Arrays. I'm not sure what the size is going to be because I am taking a randomized sample out of the first arrays and putting it into the 2nd arrays.
When I add the ReDim Preserve line I get the Subscript Out of Range error.
Function CreateTrainingSet(TrainingPercent As Double, Inputs() As Double, Outputs() As Double)
' Create Randomized Training set data
Dim TrainingInputs() As Double, TrainingOutputs() As Double
Dim i As Integer, j As Integer, count As Integer
'ReDim TrainingInputs(UBound(Inputs, 1), UBound(Inputs, 2))
'ReDim TrainingOutputs(UBound(Outputs, 1), UBound(Outputs, 2))
count = 0
' Move TraningPercent % of data from Inputs and Outputs to TrainingInputs and TrainingOutputs
For i = LBound(Inputs, 1) To UBound(Inputs, 1)
Dim ran As Double
ran = Rnd()
If ran <= TrainingPercent Then
count = count + 1
For j = LBound(Inputs, 2) To UBound(Inputs, 2)
ReDim Preserve TrainingInputs(1 To count, 1 To UBound(Inputs, 2))
TrainingInputs(count, j) = Inputs(i, j)
Next j
For j = LBound(Outputs, 2) To UBound(Outputs, 2)
ReDim Preserve TrainingOutputs(1 To count, 1 To UBound(Outputs, 2))
TrainingOutputs(count, j) = Outputs(i, j)
Next j
End If
Next i
For i = LBound(TrainingInputs, 1) To UBound(TrainingInputs, 1)
For j = LBound(TrainingInputs, 2) To UBound(TrainingInputs, 2)
Cells(i, j + 10).Value = TrainingInputs(i, j)
Next j
Next i
End Function
The ReDim statement is used to size or resize a dynamic array that has already been formally declared by using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). Use the ReDim statement repeatedly to change the number of elements and dimensions in an array.
ReDim Preserve any dimension of a 2D Array If you'd like to ReDim Preserve a multidimensional array larger than two-dimensions, your best bet is to construct your array in such a way that only the number of elements in the last dimension will need to be preserved.
Examples to use VBA Redim StatementStep 1: Create a macro name first. Step 2: Declare an array name as a string. Step 3: Now use the word “Redim” and assign the size of the array. Step 4: So now array name “MyArray” can hold up to 3 values here.
Note: Variant Arrays are a little different. With variant arrays, you don’t need to set the array size with ReDim before assigning values. The ReDim statement resizes an array, clearing all existing values. The ReDim Preserve statement resizes an array, keeping (“preserving”) all existing values. VBA Programming | Code Generator does work for you!
The ReDim Preserve statement resizes an array, keeping (“preserving”) all existing values. In practice, resizing an array with ReDim looks like this: In this example, we will use ReDim to set the initial dynamic array and then ReDim Preserve to resize the array, keeping the original values:
Always remember to add Preserve after ReDim in VBA, so that it will store the previously used dimensions. If a dimension is defined as String, then any position left blank in assigning values, will carry blank and once we print the same with the help of message box will give blank message box as well.
VBA ReDim allows us to redefine the dimension values. We can define as many as dimensional arrays with no or zero value and store the values later. ReDim meaning Re-Dimensioning gives use the allowances of adding any numbers of data arrays without increasing the size of stored data.
To summarise the comments above into an answer:
Preserve
.Therefore in order to resize a multiple dimension array there are a couple of simple options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With