ReDim保留“下标超出范围” [英] ReDim Preserve "Subscript Out of Range"

查看:184
本文介绍了ReDim保留“下标超出范围”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从2个双阵列移动到2个不同的双阵列。我不知道大小会是什么,因为我正在从第一个数组中取出一个随机抽样并将其放入第二个数组。

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.

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

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


推荐答案

将上述评论总结为答案:

To summarise the comments above into an answer:


  • 您只能重写多维数组的最后一个维度

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

Therefore in order to resize a multiple dimension array there are a couple of simple options:


  1. 如果只需要调整一个维度即可翻转循环和逻辑,以便调整大小的维度成为最后一个维度。

  2. 如果两个维度都必须调整大小,请使用阵列数组或数组集合,并根据需要更正循环。

这篇关于ReDim保留“下标超出范围”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆