Running standard deviation on matrix with NaN values
7 views (last 30 days)
Show older comments
Tereza Smejkalova
on 16 Dec 2013
Commented: Tereza Smejkalova
on 24 Jan 2014
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
0 Comments
Accepted Answer
Jos (10584)
on 17 Dec 2013
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 Comments
More Answers (2)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!