-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDistEntropy.m
More file actions
48 lines (42 loc) · 946 Bytes
/
DistEntropy.m
File metadata and controls
48 lines (42 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function Entropy=DistEntropy(X,Nbins,scaled,relative)
%Calculates the (relative) entropy of a distribution
%Semidan, April 2015
%**************************************************************************
if nargin<2,
Nbins=20;
end
if isempty(Nbins),
Nbins=20;
end
if nargin<3,
scaled='F';
end
if isempty(scaled),
scaled='F';
end
if nargin<4,
relative='F';
end
if isempty(relative),
relative='F';
end
E=zeros(Nbins,1);
p=size(X,2)/(Nbins*size(X,2));
Entropy=zeros(size(X,1),1);
for i=1:size(X,1),
[counts,~]=hist(X(i,:),Nbins);
counts=counts/sum(counts);
for j=1:Nbins,
E(j)=counts(j)*log(counts(j));
if isnan(E(j)),
E(j)=0;
end
end
Entropy(i)=-sum(E);
if scaled=='T',
Entropy(i)=abs(max(X(i,:))-min(X(i,:)))*Entropy(i);
end
if relative=='T',
Entropy(i)=Entropy(i)/(-log(p));
end
end