《《 求相关系数的算法 》》已知有两组数据,分别存放在数组 A(100) 和 B(100)中,求计算两组数据的相关性和相

1个回答

  • Private Sub Command1_Click()

    Dim Aave As Single, Bave As Single, i As Integer, Sigma As Single, Sigma2 As Single, Rab As Single

    Dim A() As String, B() As String, myStrA As String, myStrB As String

    myStrA = "1,2,3,4,5,6,7,8" '把数组A的值赋给第一个字符串,数字间用英文逗号分开

    myStrB = "8,7,6,5,4,3,2,1" '把数组B的值赋给第另一个字符串,数字间用英文逗号分开

    A = Split(myStrA, ",")

    B = Split(myStrB, ",")

    For i = LBound(A) To UBound(A)

    Aave = Aave + Val(A(i))

    Bave = Bave + Val(B(i))

    Next

    Aave = Aave / (UBound(A) + 1)

    Bave = Bave / (UBound(B) + 1)

    For i = LBound(A) To UBound(A)

    Sigma = Sigma + (A(i) - Aave) * (B(i) - Bave)

    Sigma2 = Sigma2 + (A(i) - Aave) ^ 2 * (B(i) - Bave) ^ 2

    Next

    Rab = Sigma / Sqr(Sigma2)

    MsgBox Rab

    End Su