Why std function returns NaN?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (3)
John D'Errico
am 25 Nov. 2018
Bearbeitet: John D'Errico
am 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 Kommentare
Stephen23
am 26 Nov. 2018
Bearbeitet: Stephen23
am 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
am 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.
Star Strider
am 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 Kommentare
Star Strider
am 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).
Image Analyst
am 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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!