Be the first to rate this file! 55 Downloads (last 30 days) File Size: 1.55 KB File ID: #28692

Entropy

by

 

Calculates the sample entropy, in bits, of discrete variables.

| Watch this File

File Information
Description

Entropy: Returns entropy (in bits) of each column of 'X'

  by Will Dwinnell
 
  H = Entropy(X)
 
  H = row vector of calculated entropies (in bits)
  X = data to be analyzed
 
  Note 1: Each distinct value in X is considered a unique value.

  Note 2: Estimated entropy values are slightly less than true,
    due to finite sample size.
 
  Example:

  X = ceil(repmat([2 4 8 16],[1e3,1]) .* rand(1e3,4));
  Entropy(X)

MATLAB release MATLAB 6.0 (R12)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
27 Mar 2013 Julia Ferre

Do you think we could used this code for images? (the images should be considered as 1D vectors, of course).

29 Sep 2010 Geoff McDonald

Oh, I would just like to note that the below code will only work for 1d vectors, ie. Nx1 or 1xN.

29 Sep 2010 Geoff McDonald

I had serious trouble with the performance of this entropy calculation method. For about 500k samples it takes about 20 seconds to compute the entropy. Here is an alternative entropy function I just wrote up for integer signal values (ie. y = [1 -6011 -3000 2592]):

function [ent] = EntropyInt(y)
% Calculate the entropy for an integer value of y

% First verify that y is truely integer-valued
Sum = sum(y);
if( Sum ~= round(Sum) )
error('INTEGER_ENTROPY:InvalidInput', 'Input arguments must be of integer value.')
end

% Generate the histogram
[n x] = hist(y, double(min(y):max(y)));

% Normalize the area of the histogram to make it a pdf
n = n / sum(n);

% Calculate the entropy
indices = n ~= 0;
ent = -sum(n(indices).*log2(n(indices)));
end

Contact us