0
Sub HMM()
Dim x As Integer

Dim GG As Integer
Dim Gr As Integer
Dim rG As Integer
Dim rr As Integer

For x = 3 To x = 26126
    If ActiveSheet.Cells(x, 3) > 0 And ActiveSheet.Cells(x + 1, 3) > 0 Then
            GG = GG + 1
    End If
    If ActiveSheet.Cells(x, 3) > 0 And ActiveSheet.Cells(x + 1, 3) < 0 Then
            Gr = Gr + 1
    End If
    If ActiveSheet.Cells(x, 3) < 0 And ActiveSheet.Cells(x + 1, 3) > 0 Then
            rG = rG + 1
    End If
    If ActiveSheet.Cells(x, 3) < 0 And ActiveSheet.Cells(x + 1, 3) < 0 Then
            rr = rr + 1
    End if
 Next x

 With ActiveSheet
   .Cells(2, 30) = GG
   .Cells(3, 30) = Gr
   .Cells(4, 30) = rG
   .Cells(5, 30) = rr
End With

End Sub

所以我有一长串数字,从 C3 到 C26126。我需要做的是找出正数在正数(GG)之前多少次,负数在正数之前多少次(rG)等等。所以我需要GG Gr rG和rr。对不起,如果这还不够清楚。

示例:我有一个表 y:[-1,2,2,3,-1,-2,2] GG = 2, Gr = 1, rG = 2, rr = 1

我的最终目标是为我的时间序列找到一个简单的隐马尔可夫模型。

4

2 回答 2

1
Sub Tester()

    Dim pp, pn, np, nn, x, arr, v1, v2

    arr = ActiveSheet.Range("C3:C1110").Value
    For x = 1 To UBound(arr, 1) - 1
        v1 = arr(x, 1): v2 = arr(x + 1, 1)
        If v1 > 0 And v2 > 0 Then
            pp = pp + 1
        ElseIf v1 > 0 And v2 < 0 Then
            pn = pn + 1
        ElseIf v1 < 0 And v2 > 0 Then
            np = np + 1
        ElseIf v1 < 0 And v2 < 0 Then
            nn = nn + 1
        End If
    Next x

    Debug.Print pp, pn, np, nn

End Sub
于 2013-08-01T20:10:46.500 回答
0

不太确定问题是什么......因为你已经给出了答案

Sub HMM()
    Dim myWS As Worksheet
    Set myWS = Worksheets("Sheet1")

    Dim x As Integer

    Dim GG As Integer
    Dim Gr As Integer
    Dim rG As Integer
    Dim rr As Integer

    'Initially 0 by default but it's always good to be explicit
    GG = 0
    Gr = 0
    rG = 0
    rr = 0

    'Count from 3 to 26125 instead of from 3 to 26126
    With myWS
        For x = 3 To 26125

            If .Cells(x, 3).Value > 0 Then
                If .Cells(x + 1, 3).Value > 0 Then
                    GG = GG + 1
                ElseIf .Cells(x + 1, 3).Value < 0 Then
                    Gr = Gr + 1
                End If
            ElseIf .Cells(x, 3).Value < 0 Then
                If .Cells(x + 1, 3).Value > 0 Then
                    rG = rG + 1
                ElseIf .Cells(x + 1, 3).Value < 0 Then
                    rr = rr + 1
                End If
            End If
        Next x

        Debug.Print GG, Gr, rG, rr

    End With

End Sub

如果我是你,我会将 .Cells(x+1,3) 设置为一个变量,然后进行比较。但我不确定应该使用哪种变量类型,因为一切都有限制。

于 2013-08-01T20:21:46.603 回答