How to eliminate Nan

2 views (last 30 days)
shailesh pathak
shailesh pathak on 16 Mar 2018
Commented: Star Strider on 16 Mar 2018
Gliq=(R*T*((x(1)*log(x(1)))+(x(2)*log(x(2)))+(x(3)*log(x(3)))+(x(4)*log(x(4)))+(x(5)*log(x(5)))+(x(6)*log(x(6)))+(x(7)*log(x(7)))+(x(8)*log(x(8)))));
for x=[0.162071097716028 0 0.276531475376737 0 0.264268578570244 0.247172330667782 0.0276055958119857 0.0223509218572239];
This code produces NaNs because of taking the natural log of zeros.
Can somebody help me how to eliminate Nans?
  2 Comments
James Tursa
James Tursa on 16 Mar 2018
What would you like to have happen for those spots? Replace the NaN's with something else? Delete the NaN's from the result (i.e., shrink the size of the result)? Or ...?
shailesh pathak
shailesh pathak on 16 Mar 2018
to eliminate those Nan's

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 16 Mar 2018
One option:
x=[0.162071097716028 0 0.276531475376737 0 0.264268578570244 0.247172330667782 0.0276055958119857 0.0223509218572239];
x = x(x > 0);
  4 Comments
Image Analyst
Image Analyst on 16 Mar 2018
I'm having trouble following all those thousands of parentheses. But it looks like the RT is multiplying the sum of all the terms, so do you mean
Gliq = R*T*sum(x.*log(x));
Star Strider
Star Strider on 16 Mar 2018
That’s essentially it.
It uses ‘dot product’ (link) vector multiplication to multiply row vector ‘x’ by column vector ‘log(x(:))’.
Gliq = R*T*dot(x,log(x))
produces the same result.

Sign in to comment.


James Tursa
James Tursa on 16 Mar 2018
result = the stuff you are currently doing
result(isnan(result)) = [];
  2 Comments
shailesh pathak
shailesh pathak on 16 Mar 2018
Getting the same problem
James Tursa
James Tursa on 16 Mar 2018
"... This is not working ..." and "... Getting the same problem ..."
These comments do not help us since they give us no further detail into the issues you are having. Please show us your complete code, the current result you are getting, and then show us the result that you would like to get.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!