How to eliminate Nan

2 ビュー (過去 30 日間)
shailesh pathak
shailesh pathak 2018 年 3 月 16 日
コメント済み: Star Strider 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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by