MATLAB: How to calculate the entropy

entropy

How can I calculate the entropy of a sentence and selected sentence of a string. Thanks

Best Answer

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
Related Question