entropy.m

来自「霍夫曼coding程序」· M 代码 · 共 20 行

M
20
字号
function h = entropy(x , n)
% ENTROPY Computes a first-order estimate of the entropy of a matrix.
%   H = ENTROPY(X , N) returns the first-order estimate of matrix X
%   with N symbols (N = 256 if omitted) in bits/sybol. The estimate
%   assumes a statistically independent sourc3e characterized by the
%   relative frequency of occurrence of the elements in X.

error(nargchk(1 , 2 , nargin));     % Check input arguments
if nargin < 2
    n = 256;                        % Default for n.
end

x = double(x);                      % Make input double
xh = hist(x(:) , n);                % Compute N-bin histogram
xh = xh / sum(xh(:));               % Compute probabilities

% Make mask to eliminate 0's since log2(0) = -inf.
i = find(xh);

h = -sum(xh(i) .* log2(xh(i)));     % Compute entropy

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?