1

So, after trying a heavy cicle-based function for calculating the joint entropy of two sources of information, I found this useful MATLAB function, accumarray, and tried the following code:

function e = jointEntropy(fonte1, fonte2)
    i = double(fonte1(:))+ 1; 
    j = double(fonte2(:)) + 1; 

    subs = [i j];
    f = accumarray(subs, ones(length(fonte1), 1));
    p = f / length(fonte1);
    freq = f ~= 0;
    prob = p(freq);
    e = -sum(prob.*log2(prob));
end

, where fonte1 and fonte2 are the information sources, 1xN arrays. This worked just fine with examples in which both sources had only positive integer values, but then I tried to use it with audio files (i.e., arrays whose values ranged from -1 to 1) and it kept giving me an error. I tried adding 1 to each array (to range from 0 to 2), multiplying them by 100 and rounding the numbers, in order to obtain non-negative integers, but it still doesn't work.

Any idea/ alternative to this code would be greatly appreciated.

chappjc
  • 30,359
  • 6
  • 75
  • 132
M. M. F.
  • 35
  • 10
  • What's the error? Also please show the code you mentioned where you re-scale and round the input data. As a suggestion, you could do `e = -sum(sum(p.*full(spfun(@log2,p))));` or `e = -sum(sum(p.*(log2(p+(p==0)))));`. See what's fastest. – chappjc Oct 22 '14 at 21:06
  • Good first shot, by the way! Also, with `accumarray`, you can just do `accumarray(subs,1)` and it will use `1` for each sub. – chappjc Oct 22 '14 at 21:34
  • Hmm :) It looks like you borrowed that code from here: http://stackoverflow.com/questions/23691398/mutual-information-of-two-images-matlab :) It'd be nice if you actually sourced where you got the code from next time. Now, to address your question, you can still use that code, but consider assigning a **unique ID** to each floating point number instead. Therefore, do this to replace `subs`: `[~,~,id1] = unique(fonte1(:)); [~,~,id2] = unique(fonte2(:)); subs = [id1 id2];`. I should also update my post as well to reflect this. Thanks for the idea! – rayryeng Jan 02 '15 at 18:02
  • @rayryeng No wonder it was a good first shot. :) – chappjc Jan 02 '15 at 18:04
  • @chappjc - Hehe :) BTW, an easter egg in that post... when I mentioned `accumarray`, I thanked you in brackets. I wouldn't have learned how to use it effectively if it weren't for you.... so I should actually be thanking you! – rayryeng Jan 02 '15 at 18:05
  • @rayryeng Well thank you for that! But reading that part of your post I detect a suggestion that I may be a bit obsessed with `accumarray`... guilty as charged! :D – chappjc Jan 02 '15 at 18:09
  • @chappjc - hehe... well, your profile does say you <3 `accumarray`... I'm trying to share the love too ;) – rayryeng Jan 02 '15 at 18:12

0 Answers0