Using Matlab 3.5. Suppose a data source produces a series of characters drawn fr
ID: 3753859 • Letter: U
Question
Using Matlab
3.5. 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 k-1 Essentially Hi is the number of bits needed per symbol to encode a long mes- sage; that is, it measures the amount of information content, and therefore the potential success of compression strategies. The value Hi -0 corre- sponds to the case of only one symbol being produced-no information- while if all M symbols have equal probability, then H1 -log2 M. Write a function [H,M) entropy (v) that computes entropy for a vector v. The probabilities should be computed empirically by finding the unique entries (using unique), then counting the occurrences of each symbol and dividing by the length of v. Try your function on some built-in image data by entering load clown, v -X(:)Explanation / Answer
SOLUTION:
Function [H,M]=entropy(v)
c1=unique(v);
M=length(c1);
for i=1:M
cnt=0;
for j=1:length(v)
if v(j)==c1(i)
cnt=cnt+1;
else
end
end
z(i)=cnt;
end
prb=z/length(v);
h1=0;
for i=1:M
h=prb(i)*log2(prb(i));
h1=h1+h;
end
H=h1;
end
%%EXAMPLE:
clear all
close all
load clown
v=X(:);
[H<M]=entropy(v);
fprintf(" the value of H is %2.1 if and M is %2.1 f ',H,M);
x=[10 10 20 20];
y=[10 20 20 10];
a=polyarea(x,y);
b=polyperim(x,y);
fprintf(" the area of rectangle is %2.1 f ',a);
fprintf(" the perimeter of rrectangle is %2.1 f ',a);
[the value of H is 5.1 and M is 81.0
the area of rectangle is 100.0
the perimeter of rectangle is 40.0]
FOR PERIMETER OF RECTANGLE
function p=polyperim(x,y)
p=0;
z=length(x);
x(z+1)=x(1);
y(z+1)=y(1);
for i=1:z
pp=sqrt((x(i+1)-x(i).^2+(y(i+1)-y(i).^2);
p=p+pp;
end
end
FOR AREA OF RECTANGLE
function a=polyarea(x,y)
a=0;
z=length(x);
x(z+1)=x(1);
y(z+1)=y(1);
for i=1:z
ar=(x(i)*y(I+1)-x(i+1)*y(i));
a=a+ar;
end
a=(1/2)*abs(a);
end
p=p+pp;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.