2017-03-31 107 views
-4

我正在嘗試創建一個大小爲N的vba數組,並添加100%的正態分佈百分比。這意味着輸入一個整數N時,我應該得到一個n大小的數組,其數值總和爲100,並且是正態分佈的使用正態分佈創建數組

我該怎麼做?

謝謝!

+0

@ScottCraner ......我同意,但它是一個非常簡單的代碼,只是想正態分佈部分一些幫助,我不知道該怎麼辦 – jhugo

+0

快速搜索的「填充陣列正常發行vba「給了很多起點。 –

回答

2

我很好奇,所以我做了一個。

Sub NDArray() 
Dim arr() As Double 
a = InputBox("Number of numbers") 

ReDim arr(a - 1) As Double 
With Application.WorksheetFunction 
    'fill the array with random numbers that fit a normal dist 
    For i = 0 To a - 1 
     '"A/100" is the target mean and "A/200" is target Std. Dev, change to your desire 
     arr(i) = .Norm_Inv(.RandBetween(1, 1000)/10000, a/100, a/200) 
    Next i 
    'change the numbers to percentage of whole and multiply by 100 to get the values to sum to 100 
    x = .Sum(arr) 
    For i = 0 To a - 1 
     arr(i) = (arr(i)/x) * 100 
    Next i 
    'post the numbers in Column A on the active sheet 
    ActiveSheet.Range("A:A").ClearContents 
    ActiveSheet.Range("A1").Resize(a).Value = .Transpose(arr) 
End With 
End Sub 
+0

謝謝@ScottCraner ! – jhugo

0

嘗試以下:

Sub NDArray() 
a = InputBox("enter the integer") 
ReDim arr(a - 1) 
For i = 0 To a - 1 
arr(i) = 100/a 
Next i 
End Sub 

但我也同意斯科特...嘗試先寫代碼,並發表您的問題...

+0

這將返回一個相同數字的數組。 OP希望它[正常分佈](http://www.stat.yale.edu/Courses/1997-98/101/normal.htm)。這對於OP來說是一個好的開始,但是這不會做所要求的。 –

+1

哦,我錯過了正常分佈部分的問題... –

+0

非常感謝你@AvinashKumar ...我有相同的代碼,與正常分佈的部分是我不知道該怎麼辦 – jhugo