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;
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
add a comment |
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
What is the value of yourfreq_norm
vector? I triedfreq_norm = 0.2 * ones(1, 5);
and didn't get any NaNs.
– LarrySnyder610
Mar 23 at 13:33
2
Iffreq_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
add a comment |
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
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
matlab
asked Mar 23 at 13:27
Nalin KambojNalin Kamboj
299
299
What is the value of yourfreq_norm
vector? I triedfreq_norm = 0.2 * ones(1, 5);
and didn't get any NaNs.
– LarrySnyder610
Mar 23 at 13:33
2
Iffreq_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
add a comment |
What is the value of yourfreq_norm
vector? I triedfreq_norm = 0.2 * ones(1, 5);
and didn't get any NaNs.
– LarrySnyder610
Mar 23 at 13:33
2
Iffreq_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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 23 at 13:34
medicine_manmedicine_man
27113
27113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What is the value of your
freq_norm
vector? I triedfreq_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