MATLAB: Error using hist function for entropy calculation

entropyerrorhisthistogram

Hi,
I am currently trying to write a function to calculate the Shannon entropy of some data, however, am getting the following error:
Requested 10000000001×1 (74.5GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information. Error in linspace (line 33) y = d1 + (0:n1).*(d2 – d1)./n1; Error in hist (line 96) edges = linspace(miny,maxy,x+1);||||_
The code I have currently is as follows:
N = 1000;
D = 10;
Nbinsizes = 5;
% create a random white noise signal
x = randn( 1, N);
nbins = logspace( 10, Nbinsizes, N / 10);
Histogram_Shannon_Entropy = nan( 1, Nbinsizes);
for i = 1:Nbinsizes
p = hist( x, nbins( i))/N;
Histogram_Shannon_Entropy( i) = sum( -( p( p > 0) .* ( log2( p ( p > 0)))));
end
I'm struggling to understand what the cause of this error is, so any help is greatly appreciated! Thank you kindly in advance.

Best Answer

You have
nbins = logspace( 10, Nbinsizes, N / 10);
logspace(A, B, N) is equivalent to
10.^linspace(A, B, N)
so you are asking for 10.^(10, 5, 100)
The first entry of that is going to be 10^10 . And you are asking for that to be used as the number of bins in your hist() call.