如何得到字母字符Shannon熵的总和

时间:2014-11-30 20:04:35

标签: vba word-vba entropy

我试图将word文档中所有字母字符的所有香农熵加起来。 它没有添加字符,而是给出了我为字符(27)作为答案的内容。

Dim characters(1 To 27) As Double
Dim x As Integer 'for looping
Dim tot As Double 'The final value

characters(1) = 0.1859 'Space
characters(2) = 0.0856 'A
characters(3) = 0.0139 'B
characters(4) = 0.0279 'C
characters(5) = 0.0378 'D
characters(6) = 0.1304 'E
characters(7) = 0.0289 'F
characters(8) = 0.0199 'G
characters(9) = 0.0528 'H
characters(10) = 0.0627 'I
characters(11) = 0.0013 'J
characters(12) = 0.0042 'K
characters(13) = 0.0339 'L
characters(14) = 0.0249 'M
characters(15) = 0.0707 'N
characters(16) = 0.0797 'O
characters(17) = 0.0199 'P
characters(18) = 0.0012 'Q
characters(19) = 0.0677 'R
characters(20) = 0.0607 'S
characters(21) = 0.1045 'T
characters(22) = 0.0249 'U
characters(23) = 0.0092 'V
characters(24) = 0.0149 'W
characters(25) = 0.0017 'X
characters(26) = 0.0199 'Y
characters(27) = 0.0008 'Z

For x = 1 To 27
    tot = tol + characters(x)
Next

MsgBox "The Shannon entropy of the alphabetic characters in this document is " & tot 

我得到了什么

今天是美好的一天

The Shannon entropy of the characters in this document is 0.0008

我想要的是什么

今天是美好的一天

 The Shannon entropy of the characters in this document is 1.2798542258337

1 个答案:

答案 0 :(得分:1)

我不知道你是否注意到你写了这个:

For x = 1 To 27
    tot = tol + characters(x)
Next

......虽然你可能想写这个:

For x = 1 To 27
    tot = tot + characters(x)
Next

事实上,你想要的是 tot 迭代地为自己总结一个新值。但是如果你写的话

tot = tol + characters(x)

...会发生的是tol总是= 0(因为它不保留它的值并默认获得0值)所以tot显然等于0 +最后一个元素,因为它也没有保留其更改。这是一个错字,只需用tot = tol + characters(x)更改tot = tot + characters(x),代码即可。