gpt4 book ai didi

arrays - ReDim 保留 "Subscript Out of Range"

转载 作者:行者123 更新时间:2023-12-01 18:45:42 24 4
gpt4 key购买 nike

我正在尝试将数据从 2 个 double 组移动到 2 个不同的 double 组。我不确定大小是多少,因为我正在从第一个数组中随机抽取样本并将其放入第二个数组中。

当我添加 ReDim Preserve 行时,出现下标超出范围错误。

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

最佳答案

将上述评论总结为答案:

  • 使用 Preserve 时,只能重新调整多维数组的最后一个维度。

因此,为了调整多维数组的大小,有几个简单的选项:

  1. 如果只需要调整一个维度的大小,请翻转循环和逻辑,以便要调整大小的维度成为最后一个维度
  2. 如果必须调整两个维度的大小,请使用数组的数组或数组的集合,并根据需要更正循环

关于arrays - ReDim 保留 "Subscript Out of Range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com