NaN when calculating average
古いコメントを表示
I read data from a .xls file using xlsread. There are some 3000 rows with price data, all in number format in excel. I further try to calculate the average of previous 10 days for every day using mean2(prices(i-10,1):prices(i,1)). I do this for last 2990 rows. surprisingly a lot of these throw NaN. I have also run a check "isnan" on each element in prices and none of them tests positive. also stumbled on something:
isnumeric(NaN)
ans =
1
how is this working??
採用された回答
その他の回答 (2 件)
Andrew Newell
2012 年 1 月 22 日
You could use nanmean to ignore the NaN's while calculating means:
prices = rand(20,20);
n = size(prices,1); m = size(prices,2); window=10;
runningMean = zeros(n-window,m);
for ii=1:n-window
runningMean(ii,:) = nanmean(runningMean(ii:ii+window-1,:));
end
Note that I am interpreting your statement of "previous 10 days" as the ten days before the current day. Your version is actually averaging 11 days.
6 件のコメント
David Young
2012 年 1 月 22 日
Andrew: I don't think he or she has got NaNs in his or her raw data. The NaNs are being generated because he or she is taking the mean of an empty matrix, which happens (in his or her code) whenever the price has decreased in any 10-day stretch.
Atakan
2012 年 1 月 22 日
Hi David;
I want to write a Matlab code for generating “m” numbers from N(0,1) using following algorithm. My code should be a general code for “m”, “mu” & “sigma”.
1.Generate u1, u2 from UNIF(0,1), then set y=tan(pi*(u1-1/2))
2.If u2 <= (sqrt(e)/2)*(1+y^2)*e^((-y^2)/2) then set x=y, otherwise go to step 1
3.Repeat 1-2 until you generate m numbers.
David Young
2012 年 1 月 22 日
Atakan: It really does need to be in a different question, as it's not related to this one. Even if it was, I would expect you to show what you'd done so far, and explain where you get stuck.
Atakan
2012 年 1 月 22 日
I am trying to ask it. But it gives an error such that "we are sorry but something went wrong". What can I do? Can you ask it for me? This is very important for me...
Atakan
2012 年 1 月 22 日
This is my code:
function [randnormal]=atakan(a,b,m)
randnormal=[];
count=1;
while (count<=m)
R = normrnd(a,b);
u1=unifrnd(0,1);
u2=unifrnd(0,1);
y=tan(pi*(u1-1/2));
if (u2<=((sqrt(exp(1))/2)*(1+y^2)*(exp(1)^(-y^2/2))))
randnormal=[R;y];
end
count=count+1;
end
Pankaj
2012 年 1 月 23 日
Atakan
2012 年 1 月 22 日
0 投票
Hi; I want to ask a question to the main page. But it gives an error such that "we are sorry but something went wrong". What can I do?
3 件のコメント
Andrew Newell
2012 年 1 月 22 日
I tried creating a test question and had no problem. I would suggest trying again.
Atakan
2012 年 1 月 22 日
I have tried many many times. But there is still a problem. If I send the question to you, can you ask the question for me? Because this is very important for me. thanks...
Jan
2012 年 1 月 22 日
Dear Atakan: The servers have severe problems currently.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!