以二进制序列查找平均重复数

我在描述这个问题时遇到了麻烦,所以这里是一个例子:

比方说,我有自己的行中的每个数字的Excel中的二进制序列:

10110100111011

我想弄清楚平均重复长度。

所以对于这个例子中的1,我们有:1,2,1,3,2(Avg:1.8)

你正在寻找一个公式或VBAmacros?

如果VBAmacros,那么这里是下面的代码 。 如果需要常规的Excel函数,您可以随时创build一个UDF:

Sub TestBin() x = AvgBin("111001110") 'Returns 3 End Sub Function AvgBin(binString As String) Dim count As Integer, arr() As Integer, first As Boolean first = True ReDim arr(0 To 0) For i = 1 To Len(binString) If CInt(Mid(binString, i, 1)) = 1 Then count = count + 1 Else If first And count > 0 Then arr(UBound(arr)) = count first = False Else If count > 0 Then ReDim Preserve arr(0 To UBound(arr) + 1) arr(UBound(arr)) = count End If End If count = 0 End If Next i If count > 0 Then arr(UBound(arr)) = count count = 0 End If Dim sum As Integer For i = 0 To UBound(arr) sum = sum + arr(i) Next i AvgBin = sum / (UBound(arr) + 1) End Function 

考虑这个小macros:

 Sub dural() st = "10110100111011" Dim zum As Double, kount As Double Dim av As Double ary = Split(st, "0") zum = 0 kount = 0 For Each a In ary If a <> "" Then zum = zum + Len(a) kount = kount + 1 End If Next a av = zum / kount MsgBox av End Sub