【发布时间】:2021-02-10 12:45:07
【问题描述】:
我是 VBA 和 Stack Overflow 的新手,对任何笨拙表示歉意。
根据下面的代码,我生成 Array1 的随机值,其大小由用户通过输入框指定。然后将 Array1 输出到工作表以进行测试。出于速度原因,我正在尽量减少与工作表的交互。
问题:如何计算 Array1 的每一列的平均值(这些平均值输入到要创建的 Array2 中,测量 1 行和与 Array1 中的列数相同的宽度)?计算严格在内存中完成,而不是在工作表中。每列分别取平均值。然后将这些 Array2 结果粘贴到工作表中粘贴 Array1 的位置下方的第二个空行中? (您会看到 Array1 粘贴从单元格 A1 开始并从那里向下/向右扩散)。
我已经研究并尝试过,但没有成功。运行以下代码的简单输入(对我来说很好)是 10 行,2 列,alpha = 1,beta = 1
Sub MC()
' Clear contents of active worksheet
Cells.Clear
' Moves cursor to starting point
Range("A1").Select
' Declarations of variables and arrays
Dim CellsDown As Long, CellsAcross As Long
Dim Alpha As Double, Beta As Double
Dim i As Long, j As Integer
Dim StartTime As Double
Dim Array1() As Double
Dim OutputSim As Range
' Set array dimensions and other inputs for running Monte Carlo
CellsDown = InputBox("How many rows?")
If CellsDown = 0 Then Exit Sub
CellsAcross = InputBox("How many columns?")
If CellsAcross = 0 Then Exit Sub
Alpha = InputBox("Distribution shape alpha value?")
If Alpha <= 0 Then Exit Sub
Beta = InputBox("Distribution shape beta value?")
If Beta <= 0 Then Exit Sub
' Record starting time
StartTime = Timer
' Redimension array
ReDim Array1(1 To CellsDown, 1 To CellsAcross)
' Set worksheet range
Set OutputSim = ActiveCell.Range(Cells(1, 1), Cells(CellsDown, CellsAcross))
' Fill array1 with random values generated from inverse of cumulative beta probability density function
Application.ScreenUpdating = False
For i = 1 To CellsDown
For j = 1 To CellsAcross
Randomize
Array1(i, j) = Application.Beta_Inv(Application.RandBetween(0.0000001, 100 - 0.0000001) / 100, Alpha, Beta, 0, 1)
Next j
Next i
' Transfer array1 to worksheet
OutputSim.Value = Array1
' Display elapsed time
Application.ScreenUpdating = True
MsgBox Format(Timer - StartTime, "00.00") & " seconds"
End Sub
【问题讨论】: