Variable assigned in NaN in loop iterationSumming 2D vectordifferent sized bins in matlabMatlab: How I can make this transformation on the matrix A? (part 2)Basic file I/O in MatlabFaster way of implementing-To assign label for every pixelPlot specific jumps in value range of a matrixShannon's entropy for non-equiprobable occurence of symbols in a blockHow to compute double sum in Matlab where upper bound of second sum is lower bound of first sum?Ignore NaN when detrending 3-d arrayUsing Matlab's lsqcurvefit to Calculate Infinite Relaxation Spectrum

What's the difference between "за ... от" and "в ... от"?

Why was Thor doubtful about his worthiness to Mjolnir?

How does emacs `shell-mode` know to prompt for sudo?

Could there be a material that inverts the colours seen through it?

In books, how many dragons are there in present time?

How much Replacement does this axiom provide?

CPLD based Pierce oscillator

Automatically anti-predictably assemble an alliterative aria

Magento 2: How to get type columns of table in sql?

correct spelling of "carruffel" (fuzz, hustle, all that jazz)

Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?

How can dragons propel their breath attacks to a long distance

How do employ ' ("prime") in math mode at the correct depth?

Jesus' words on the Jews

Why is tomato paste so cheap?

Anabelian geometry ~ higher category theory

Missouri raptors have wild hairdos

Is Germany still exporting arms to countries involved in Yemen?

On studying Computer Science vs. Software Engineering to become a proficient coder

As programers say: Strive to be lazy

Extracting sublists that contain similar elements

Can I say that master can only initiate communication in SPI vs. in I2C slave can also initiate the communication?

Labeling matrices/rectangles and drawing Sigma inside rectangle

Why would a switch ever send an ARP request for a MAC address when it can just wait for the first packet to be received from a device?



Variable assigned in NaN in loop iteration


Summing 2D vectordifferent sized bins in matlabMatlab: How I can make this transformation on the matrix A? (part 2)Basic file I/O in MatlabFaster way of implementing-To assign label for every pixelPlot specific jumps in value range of a matrixShannon's entropy for non-equiprobable occurence of symbols in a blockHow to compute double sum in Matlab where upper bound of second sum is lower bound of first sum?Ignore NaN when detrending 3-d arrayUsing Matlab's lsqcurvefit to Calculate Infinite Relaxation Spectrum






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to calculate Entropy manually and my variable to store the result gets a NaN value at every iteration. I have the following code snippet in a matlab function -



entropy = 0.0;
%Calculating entropy...
for i = 1:size(freq_norm)
val = freq_norm(i);
val_log = log2(val);
mul = val * val_log;
entropy = entropy - mul;
disp(['VALUE: ',num2str(entropy)]);
end


Here freq_norm is a vector which consists of values which sum to 1 (like a pdf).
I have the exact same snippet in another part of the program which goes 1:6 values and works perfectly.
I have also printed out all val, val_log, mul values and none of them are NaN and print as expected. mul contains values which are approximately about -0.05.



Any help appreciated. Thanks!










share|improve this question






















  • What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

    – LarrySnyder610
    Mar 23 at 13:33






  • 2





    If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

    – Cris Luengo
    Mar 23 at 13:34











  • Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

    – Nalin Kamboj
    Mar 23 at 18:02

















0















I am trying to calculate Entropy manually and my variable to store the result gets a NaN value at every iteration. I have the following code snippet in a matlab function -



entropy = 0.0;
%Calculating entropy...
for i = 1:size(freq_norm)
val = freq_norm(i);
val_log = log2(val);
mul = val * val_log;
entropy = entropy - mul;
disp(['VALUE: ',num2str(entropy)]);
end


Here freq_norm is a vector which consists of values which sum to 1 (like a pdf).
I have the exact same snippet in another part of the program which goes 1:6 values and works perfectly.
I have also printed out all val, val_log, mul values and none of them are NaN and print as expected. mul contains values which are approximately about -0.05.



Any help appreciated. Thanks!










share|improve this question






















  • What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

    – LarrySnyder610
    Mar 23 at 13:33






  • 2





    If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

    – Cris Luengo
    Mar 23 at 13:34











  • Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

    – Nalin Kamboj
    Mar 23 at 18:02













0












0








0








I am trying to calculate Entropy manually and my variable to store the result gets a NaN value at every iteration. I have the following code snippet in a matlab function -



entropy = 0.0;
%Calculating entropy...
for i = 1:size(freq_norm)
val = freq_norm(i);
val_log = log2(val);
mul = val * val_log;
entropy = entropy - mul;
disp(['VALUE: ',num2str(entropy)]);
end


Here freq_norm is a vector which consists of values which sum to 1 (like a pdf).
I have the exact same snippet in another part of the program which goes 1:6 values and works perfectly.
I have also printed out all val, val_log, mul values and none of them are NaN and print as expected. mul contains values which are approximately about -0.05.



Any help appreciated. Thanks!










share|improve this question














I am trying to calculate Entropy manually and my variable to store the result gets a NaN value at every iteration. I have the following code snippet in a matlab function -



entropy = 0.0;
%Calculating entropy...
for i = 1:size(freq_norm)
val = freq_norm(i);
val_log = log2(val);
mul = val * val_log;
entropy = entropy - mul;
disp(['VALUE: ',num2str(entropy)]);
end


Here freq_norm is a vector which consists of values which sum to 1 (like a pdf).
I have the exact same snippet in another part of the program which goes 1:6 values and works perfectly.
I have also printed out all val, val_log, mul values and none of them are NaN and print as expected. mul contains values which are approximately about -0.05.



Any help appreciated. Thanks!







matlab






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 13:27









Nalin KambojNalin Kamboj

299




299












  • What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

    – LarrySnyder610
    Mar 23 at 13:33






  • 2





    If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

    – Cris Luengo
    Mar 23 at 13:34











  • Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

    – Nalin Kamboj
    Mar 23 at 18:02

















  • What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

    – LarrySnyder610
    Mar 23 at 13:33






  • 2





    If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

    – Cris Luengo
    Mar 23 at 13:34











  • Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

    – Nalin Kamboj
    Mar 23 at 18:02
















What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

– LarrySnyder610
Mar 23 at 13:33





What is the value of your freq_norm vector? I tried freq_norm = 0.2 * ones(1, 5); and didn't get any NaNs.

– LarrySnyder610
Mar 23 at 13:33




2




2





If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

– Cris Luengo
Mar 23 at 13:34





If freq_norm contains a 0, the log will be -Inf, which you then multiply by 0 to get NaN. You need to skip the zeros to compute entropy.

– Cris Luengo
Mar 23 at 13:34













Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

– Nalin Kamboj
Mar 23 at 18:02





Turns out it did contain a 0. It has 256 values and I missed checking a couple of them. Thanks!

– Nalin Kamboj
Mar 23 at 18:02












1 Answer
1






active

oldest

votes


















1














The log2 function is returning NaN, probably because at some point you are trying to take the log of 0 (which is -Inf) and multiplying by 0.



See this example for reference.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314202%2fvariable-assigned-in-nan-in-loop-iteration%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The log2 function is returning NaN, probably because at some point you are trying to take the log of 0 (which is -Inf) and multiplying by 0.



    See this example for reference.






    share|improve this answer



























      1














      The log2 function is returning NaN, probably because at some point you are trying to take the log of 0 (which is -Inf) and multiplying by 0.



      See this example for reference.






      share|improve this answer

























        1












        1








        1







        The log2 function is returning NaN, probably because at some point you are trying to take the log of 0 (which is -Inf) and multiplying by 0.



        See this example for reference.






        share|improve this answer













        The log2 function is returning NaN, probably because at some point you are trying to take the log of 0 (which is -Inf) and multiplying by 0.



        See this example for reference.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 13:34









        medicine_manmedicine_man

        27113




        27113





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314202%2fvariable-assigned-in-nan-in-loop-iteration%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript