NaN issue when calculating for a mean value

73 次查看(过去 30 天)
gh = 0;
fg = 1;
mu_fg = MU_fib/fg;
adi = 1;
w = 0.042;
for ix = 1:200
for iy = 1:200
gh = gh + 1;
D = phi.*exp(MU_fat).*mu_fg.*((1-exp(-mewelement.*w))/mewelement).*((fg+adi)/adi).*(1/w);
meanD = sum(D)/60;
end
Above is a section of the program I am writing. This section has the goal of find the dose for each section of tissue. However, when i run it through, I get dose as NaN. I want the mean dose for the entire phantom as a single number.
Without the line meanD = sum(D)/60;, I get a matrix of 1 x 60 values which i don't want. I want one value for the dose (D).
Thank you. Any help is appreciated
  1 个评论
Guillaume
Guillaume 2020-1-23
编辑:Guillaume 2020-1-23
Unfortunately, we don't know what MU_fib, MU_fat, phi and mwelement are, so can't test your code. You're getting NaN because one of your expression result in either
  • 0 / 0
  • Inf / Inf
  • 0 * Inf
  • Inf - Inf or similar operation
or one of your input is NaN already.
"I get a matrix of 1 x 60 values which i don't want" If you didn't expect a vector, then clearly you've got a bug. You'll get a vector if any of the above variables are a vector.
Note that as written, your loops overwrite meanD and D at each step, so you'll end up with just the value for ix = 200 and iy = 200 at the end.

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2020-1-23
The NaN result is due to one or more elements of the vector you are taking the mean of being (0/0) or (Inf/Inf), since they evaluate to NaN.
There are three options:
(1) Find and eliminate the source of the NaN elements
(2) Use the approach in Mean Excluding NaN, or the nanmean equivalent
(3) use D(~isnan(D)) as the argument to mean in your present code.

类别

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

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by