How to eliminate Nan

2 次查看(过去 30 天)
shailesh pathak
shailesh pathak 2018-3-16
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 个评论
James Tursa
James Tursa 2018-3-16
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 2018-3-16
to eliminate those Nan's

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2018-3-16
One option:
x=[0.162071097716028 0 0.276531475376737 0 0.264268578570244 0.247172330667782 0.0276055958119857 0.0223509218572239];
x = x(x > 0);
  4 个评论
Image Analyst
Image Analyst 2018-3-16
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 2018-3-16
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.

请先登录,再进行评论。


James Tursa
James Tursa 2018-3-16
result = the stuff you are currently doing
result(isnan(result)) = [];
  2 个评论
shailesh pathak
shailesh pathak 2018-3-16
Getting the same problem
James Tursa
James Tursa 2018-3-16
"... 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by