How to calculate the entropy?

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

 Accepted 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

13 Comments

Thanks Sir..
Glad to be of help.
str = 'A quick brown fox. It can run fast';
Sir I want to calculate the entropy for both the sentences individually.
Thanks
You can split the string into sentences using strsplit() and then calculate entropy
str = 'A quick brown fox. It can run fast';
sentences = strsplit(str, '.');
entropy_sentences = cellfun(@(x) entropy(uint8(x)), sentences)
Ok sir thanks
मैं भारत से प्यार करता हूँ |
गुरुदेव रवींद्र नाथ ठाकुर भारत के बँगला साहित्य के शिरोमणि कवि थे |
The above solution is not working for this language(Hindi). I want to embed entropy of first sentence after first sentence and entropy of second sentence after second sentence.
Thanks
Something like this?
str = 'मैं भारत से प्यार करता हूँ | गुरुदेव रवींद्र नाथ ठाकुर भारत के बँगला साहित्य के शिरोमणि कवि थे |';
sentences = strsplit(str, '|');
sentences = sentences(~(cellfun(@(x) isempty(x), sentences)));
entropy_sentences = cellfun(@(x) {num2str(entropy(uint8(x)))}, sentences);
comb_vec = cell(1, numel(sentences)*2);
comb_vec(1:2:end) = sentences;
comb_vec(2:2:end) = entropy_sentences;
strjoin(comb_vec, '')
Thanks sir.
s = struct('zero', {strcat(char(8206),char(8206))},'one',{strcat(char(8206),char(8236))},'two',{strcat(char(8206),char(8237))},'three',{strcat(char(8206),char(8204))},'four',{strcat(char(8204),char(8206))},'five',{strcat(char(8204),char(8236))},'six',{strcat(char(8204),char(8237))},'seven',{strcat(char(8236),char(8236))},'eight',{strcat(char(8236),char(8204))},'nine',{strcat(char(8236),char(8237))},'dot', {strcat(char(8204),char(8204))});
final = ' ';
for i =1:size(entropy_sentences,2)
val_final='';
ent = (entropy_sentences(1,i))
input('check')
for j = 1:size(ent,2)
if(isequal(ent(1,j),0))
val = getfield(s,'zero')
input('check 0 ');
elseif(isequal(ent(1,j),1))
val = getfield(s,'one')
elseif(isequal(ent(1,j),2))
val = getfield(s,'two')
elseif(isequal(ent(1,j),3))
val = getfield(s,'three')
elseif(isequal(ent(1,j),4))
val = getfield(s,'four')
input('check 4 ');
elseif(isequal(ent(1,j),5))
val = getfield(s,'five')
elseif(isequal(ent(1,j),6))
val = getfield(s,'six')
elseif(isequal(ent(1,j),7))
val = getfield(s,'seven')
elseif(isequal(ent(1,j),8))
val = getfield(s,'eight')
elseif(isequal(ent(1,j),9))
val = getfield(s,'nine')
input('check 9');
elseif(isequal(ent(1,j),'.'))
val = getfield(s,'dot')
input('dot')
end
val_final = [val_final,val];
end
end
I tried this code to convert this entropy into Zero width code but its not working. There is any method to covert the entropy before embedding process.
I am not sure clear about your question. Can you give more details or better it might be better to start a new question so that it will get more attention from others.
Ok sir, let me explain..
str = 'मैं भारत से प्यार करता हूँ | गुरुदेव रवींद्र नाथ ठाकुर भारत के बँगला साहित्य के शिरोमणि कवि थे |';
I sent you this string and asked how to calculate of this string.
then you replied with the following code.
sentences = strsplit(str, '|');
sentences = sentences(~(cellfun(@(x) isempty(x), sentences)));
entropy_sentences = cellfun(@(x) {num2str(entropy(uint8(x)))}, sentences);
comb_vec = cell(1, numel(sentences)*2);
comb_vec(1:2:end) = sentences;
comb_vec(2:2:end) = entropy_sentences;
strjoin(comb_vec, '')
I want to convert the entropy into Zero Width Characters before the embedding process.
Thanks sir.
Do you want to replace entropy value with zero width character?
yes to make it invisible

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!