How to eliminate Nan
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
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
      
      
 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 ...?
답변 (2개)
  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
      
      
 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
      
      
 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
      
      
 2018년 3월 16일
        result = the stuff you are currently doing
result(isnan(result)) = [];
댓글 수: 2
  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 Exchange에서 Data Type Conversion에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



