My final output matrix contains NaN values

36 views (last 30 days)
I have written a code where there are 100 iterations, and at the end I get a matrix (14680*101). for some strange reason, some elements of this matrix are NaN. The simulation ran fine, no error message in matlab, and it didn't stop. Does anyone have any idea what this means? an row example: Iteration1.........Iteration 35 Iteration 36.........Iteration 100 Row1: 12.21121...........35.546654799912 NaN ( from this point and on, they are all NaN)
  1 Comment
Stephen23
Stephen23 on 8 Feb 2016
You need to check the calculation. NaN's do not mean "error", they mean that the result of a calculation is not defined. Check for divide-by-zero, multiply by infinity, etc. The nan documentation lists all of the things you need to check for:
  • Any arithmetic operation on a NaN, such as sqrt(NaN)
  • Addition or subtraction, such as magnitude subtraction of infinities as (+Inf)+(-Inf)
  • Multiplication, such as 0*Inf
  • Division, such as 0/0 and Inf/Inf
  • Remainder, such as rem(x,y) where y is zero or x is infinity

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Feb 2016
We need to see the code.
If you have a loop in which one iteration depends on the previous value then it is almost certain that as soon as one Nan is produced that everything else will be nan.
You get nan from 0/0 or 0*infinity. Do you have log of an exponential value? Too large of an exponent would lead to infinity and log cannot recover the original exponent when that happens.
  2 Comments
Amine Ben Ayara
Amine Ben Ayara on 8 Feb 2016
Good morning Roberts: In one of the functions that is called within the loop, there is a formula that uses an Exp. So let me tell you, I ran the simulation 101 times ( i=0:100) and my final matrix had NaN Values. But I went and did the simulation manually; 101 times by changing i, and I got a matrix that didn't contain any NaN values. So I am a bit confused here, I guess my biggest concern is to determine the part in the simulation where if the output from one iteration is used in the next or the following iteration uses the original input values again. When I did it manually, I always went back to use the original input values ( The simulation is pretty simple; only thing that changes is: one column of one of my input matrices is increases by 1 , hence from 0to 100).
Walter Roberson
Walter Roberson on 8 Feb 2016
Would you consider showing us the code instead of vaguely describing it?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!