Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Suppose a data source produces a series of characters drawn from a set of

ID: 3850128 • Letter: M

Question

MATLAB

Suppose a data source produces a series of characters drawn from a set of M distinct symbols. If symbol k is produced with probability pk, the first-order entropy of the source is defined as

Essentially H1 is the number of bits needed per symbol to encode a long message; that is, measures the amount of information content, and therefore the potential success of compression strategies. The value H1 = 0 corresponds to the case of only one symbol being produced--no information-- while if all M symbols have equal probability, then H1 = log2M .

Write a function [H,M] = entropy(v) that computes entropy for a vector v. The probabilities should be computed emperically by finding the unique entries (using unique), then counting the occurences of each symbol and dividing by then legnth of v. Try your function on some built-in image data by entering load clown, v = X(:); .

H1 1pklogopk

Explanation / Answer


function [H,M]=entropy(v)
v1=unique(v);
occurance=zeros(1,size(v1,2));
for i=1:size(v,2)
occurance(v1==v(i))=occurance(v1==v(i))+1;
end
probability=occurance./numel(v);
M=numel(v1);
H=-sum(probability.*log2(probability));