Why std function returns NaN?

51 views (last 30 days)
Matthew Wong
Matthew Wong on 21 Nov 2018
Commented: John D'Errico on 26 Nov 2018
I have a set of 100 data points that I wish to find the standard deviation of. For some reason the std function returns NaN for certain chunks of the data. Particularly, I get normal values when going through using std on the data individually until I get to value 40. Why might this be? Thanks!

Answers (3)

John D'Errico
John D'Errico on 25 Nov 2018
Edited: John D'Errico on 25 Nov 2018
LOOK AT WHAT YOU ARE DOING.
R(7)
ans =
49.8875023348093
R(40)
ans =
47.332112186806
R(7):R(40)
ans =
1×0 empty double row vector
What is the standard deviation of an empty vector? NaN.
When you use colon with the first argument greater than the second, you get an empty vector. So you got exactly what is expected, based on what you did.
Why you are doing what you are doing, that nobody else can know, only you. Should I point out that std(R(7):R(40)) does not in fact generate the standard deviation of the numbers in R in that range?
For example, suppose I do this?
R(1):R(5)
ans =
Columns 1 through 31
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Column 32
31
>> std(R(1):R(5))
ans =
9.38083151964686
But in fact, what I did was to generate the standard deviation of the linearly increasing sequence from 0 to 31. Is that really what you wanted to do?
  4 Comments
Stephen23
Stephen23 on 26 Nov 2018
Edited: Stephen23 on 26 Nov 2018
"Since the syntax I used appears not to be the correct way to do this, how might I go about doing it?"
You should do the introductory tutorials, which teach many basic concepts that all beginners need to learn to start using MATLAB (such as how to use indexing):
As John D'Errico wrote, you also need to learn to look at what your code is doing. Code does not care what you think/believe/want/assure/hope that it is doing. It is your task to actually look at what it is really doing (which include checking output values against another method, sanity checks on intermediate values, reading the documentation, trying simpler examples to ensure that an operator does what you require, checking edge cases, etc.)
John D'Errico
John D'Errico on 26 Nov 2018
When you see an error, take apart the pieces of what you did. Don't just assume that it does what you want it to do. In this case, you should havedone exactly what I did. Then you would realize that you were doing something wrong.
Here, it looks like what you really wanted was to compute
std(R(7:40))
Of course, that is very different from std(R(7):R(40)).
It may be that stdfilt does what you need. I don't have the IPT, but I do know that the Image Animalist knows what he is doing. ;-)
As Stephen suggested, this sort of indicates that what you really need to do is start with the turorials, to give yourself a basic understanding of MATLAB, instead of diving into the deep end of the pool without knowing how to swim.

Sign in to comment.


Star Strider
Star Strider on 21 Nov 2018
Please explain ‘certain chunks of the data’. When I load ‘R’, there are no NaN values, and:
Rstd = std(R)
Rstd =
65.2138227774813
  2 Comments
Matthew Wong
Matthew Wong on 25 Nov 2018
Try running the code
stdev=std(R(7):R(40));
When I try to find the standard deviation of this section the function returns NaN when there are no NaN values in that section.
Star Strider
Star Strider on 26 Nov 2018
Try indexing it correctly, as John D’Errico notes in his Answer:
stdev=std(R(7:40))
stdev =
84.1380930755812
See the documentation section on Matrix Indexing (link), and more to the point, the documentation section on Getting Started (link).

Sign in to comment.


Image Analyst
Image Analyst on 21 Nov 2018
I get the same results as Star. R(40) = 47.332112186806 which should be fine. You will get a nan if any values of R are nan. Are you taking the stdev of the whole array, or in a moving window, like with stdfilt()?
s = load('Radii.mat')
R = s.R
sd = std(R)

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!