Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:10:49+00:00 2026-06-14T07:10:49+00:00

function [ d ] = hcompare_KL( h1,h2 ) %This routine evaluates the Kullback-Leibler (KL)

  • 0
function [ d ] = hcompare_KL( h1,h2 )
%This routine evaluates the Kullback-Leibler (KL) distance between histograms. 
%             Input:      h1, h2 - histograms
%             Output:    d – the distance between the histograms.
%             Method:    KL is defined as: 
%             Note, KL is not symmetric, so compute both sides.
%             Take care not to divide by zero or log zero: disregard entries of the sum      for which with H2(i) == 0.

temp = sum(h1 .* log(h1 ./ h2));
temp( isinf(temp) ) = 0; % this resloves where h1(i) == 0 
d1 = sum(temp);

temp = sum(h2 .* log(h2 ./ h1)); % other direction of compare since it's not symetric
temp( isinf(temp) ) = 0;
d2 = sum(temp);

d = d1 + d2;

end

my problem is that whenever h1(i) or h2(i) == 0 i’m getting inf which is as expected. however in the KL distance i’m suppose to return 0 whenever they h1 or h2 ==0 how can i do that without using a loop ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:10:51+00:00Added an answer on June 14, 2026 at 7:10 am

    To avoid having issues when any of the counts is 0, I suggest you create an index that marks the “good” data points:

    %# you may want to do some input testing, such as whether h1 and h2 are
    %# of the same size
    
    %# preassign the output
    d = zeros(size(h1));
    
    %# create an index of the "good" data points
    goodIdx = h1>0 & h2>0; %# bin counts <0 are not good, either
    
    d1 = sum(h1(goodIdx) .* log(h1(goodIdx) . /h2(goodIdx)));
    d2 = sum(h2(goodIdx) .* log(h2(goodIdx) . /h1(goodIdx)));
    
    %# overwrite d only where we have actual data
    %# the rest remains zero
    d(goodIdx) = d1 + d2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function Apple(){ this.name=apple; } function Orange(){ this.name=orange; this.apple = new Apple(); this.apple.onCalled=function(){ alert(this.name); }
function LolClass(){ this.init = function(){ button_a.bind(tap, function(){ this.refreshFields(); // doesn't work //refreshFields(); // doesn't
function Person() {} Person.prototype.population = 0; Person.prototype.constructor = function(name) { this.name = name; console.log(Name:
function removeNewsItem(id,idStamp,obj){ $('#whiteCover')show(); $('#contentOfBox').html= ('Are you sure you want ot completely remove this news
function SanitizeInput(input) { input = input.replace(/</g, &lt;); input = input.replace(/>/g, &gt;); return input; }
(function() { //do stuff })(); EDIT: I originally thought this construct was called a
function Person(name){ this.name = name; } p = new Person('John'); log(typeof p.constructor); var f
function readArgs() { while getopts i:o:p:s:l:m OPTION; do case $OPTION in i) input=$OPTARG ;;
function myClass() { this.nums = [1,2,3]; this.divisor = 2; } myClass.prototype.divideNumsByDivisor = function(){ return
function averageCalculator (numvalues) { for(i=0, i <= numvalues, i++>) { var score = prompt(input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.