NaN issue when calculating for a mean value

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 Kommentar

Guillaume
Guillaume am 23 Jan. 2020
Bearbeitet: Guillaume am 23 Jan. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Star Strider
Star Strider am 23 Jan. 2020

0 Stimmen

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.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017b

Tags

Gefragt:

am 23 Jan. 2020

Bearbeitet:

am 23 Jan. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by