How to calculate the entropy?

38 Ansichten (letzte 30 Tage)
Balkar Singh
Balkar Singh am 24 Apr. 2020
Kommentiert: Balkar Singh am 5 Mai 2020
How can I calculate the entropy of a sentence and selected sentence of a string. Thanks

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 24 Apr. 2020
Suppose you have a string
str = 'A quick brown fox';
1. If you have image processing toolbox, then try
ent = entropy(uint8(str))
ent = ComputeEntropy(str);
3. You can also write the following code which does not require any other toolbox
p = sum(str.'==unique(str))./length(str);
ent = -sum(p.*log2(p));
Same result for all three options
ent =
3.6901
  13 Kommentare
Ameer Hamza
Ameer Hamza am 5 Mai 2020
Do you want to replace entropy value with zero width character?
Balkar Singh
Balkar Singh am 5 Mai 2020
yes to make it invisible

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by