VBA : 다른 배열의 각 열에 대해 계산 된 평균을 보여주는 새 배열을 만드는 방법은 무엇입니까?

Lil 'Lou-user9788072

저는 VBA 및 Stack Overflow를 처음 사용하며 서투른 것에 대해 사과드립니다.

아래 코드에 따라 입력 상자를 통해 사용자가 지정한 크기의 임의 값으로 Array1을 생성합니다. 그런 다음 테스트 목적으로 Array1이 워크 시트에 출력됩니다. 속도상의 이유로 워크 시트와의 상호 작용을 최소화하고 있습니다.

질문 : Array1의 각 열에 대한 평균을 계산하려면 어떻게해야합니까 (이러한 평균은 생성 될 Array2에 공급되고 Array1의 열 수와 동일한 너비와 1 행 측정)? 계산은 워크 시트가 아닌 메모리에서 엄격하게 수행됩니다. 각 열에 대한 평균입니다. 그런 다음이 Array2 결과를 워크 시트에서 Array1을 붙여 넣은 바로 아래의 두 번째 빈 행에 붙여 넣습니까? (Array1 붙여 넣기가 셀 A1에서 시작되어 거기에서 아래 / 오른쪽으로 펼쳐지는 것을 볼 수 있습니다.)

나는 운없이 조사하고 시도했습니다. 아래 코드를 실행하기위한 쉬운 입력은 10 행, 2 열, 알파 = 1, 베타 = 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
FaneDuru

Array1루프 로드 한 후 다음 코드를 삽입하십시오 .

'Fill the average array:____________________________________________________________________
 Dim arrAv, arrCol
 
 ReDim arrAv(CellsAcross - 1)       '1D array to keep the columns average
 For i = 0 To UBound(Array1, 2) - 1 'iterate between the array columns
    arrCol = Application.index(Array1, 0, i + 1) 'obtain a column slice from Array1
    arrAv(i) = WorksheetFunction.Average(arrCol) 'fill the array element with the Average
 Next i
 'drop the array content at two columns to the right of OutputSim:
 OutputSim.Offset(0, 2 + OutputSim.Columns.count).Resize(1, UBound(arrAv) + 1).Value = arrAv
'____________________________________________________________________________________________

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관