In the "function kl = kldivergence (x,y)", there is a serious error !
Look at the 62 and 63 line:
ipOcurrences = numel(find(x==valuesX(i))); %computes i-ocurrences at x
iqOcurrences = numel(find(y==valuesY(i))); %computes i-ocurrences at y
Note that valuesX and valuesY are distinct values for vector x and vector y, respectively. But valuesX(i) can be different from valuesY(i). Therefore pi and qi are not probability of the same index !!!!!!