Running standard deviation on matrix with NaN values

Hello, I have large matrix where each row represents time series for one location. I need to get equally sized matrix containing running standard deviation along the row dimension. There are several function to do this but none allows for NAN values. My time series includes a large amount of gaps and I can not interpolate the data. I would appreciate any suggestions. Thank you

 採用された回答

Jos (10584)
Jos (10584) 2013 年 12 月 17 日

0 投票

function SD = nanstdrow(X)
% NANSTDROW - SD per row ignoring NaNs
tf = ~isnan(X) ; % non-nan values
X(~tf) = 0 ; % set NaN values to zero so they do not contribute to the mean
N = sum(tf,2) ; % number of elements per row
M = sum(X,2) ./ N ; % average of row
D = (X - repmat(M,1,size(X,2))).^2 ; % squared difference with mean
SS = sum(tf .* D,2) % row sum
SD = sqrt(SS./N) ; % calculate SD per row

4 件のコメント

Jos (10584)
Jos (10584) 2013 年 12 月 17 日
not thoroughly tested but it should give you an idea ..
Walter Roberson
Walter Roberson 2013 年 12 月 17 日
This uses the biased form of std rather than the unbiased form, which would divide SS by (N-1) instead of by (N)
Jos (10584)
Jos (10584) 2013 年 12 月 17 日
True! Use SD = sqrt(SS./(N-1))
Tereza Smejkalova
Tereza Smejkalova 2014 年 1 月 24 日
Thanks, in the end I solved it by using nlfilter and static function.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 12 月 16 日

2 投票

function s = nanstd(X)
s = std(X(~isnan(X)));
end
Tereza Smejkalova
Tereza Smejkalova 2013 年 12 月 17 日

0 投票

I know about the nanstd() but if I understand correctly I would have to run in in a loop within loop for each window and each row. I forgot to mention before that my table is 380 000 rows and 1000 columns. I was wondering if there is anything to use without a loop?

カテゴリ

ヘルプ センター および File ExchangeInterpolation of 2-D Selections in 3-D Grids についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by