Standard Deviation of 3D matrix with NaN elements

I have a 3D matrix of size (170 x 170 x 27). Some of the entries are NaN, zero and the rest have real values.
I would like to obtain the standard deviation of this matrix, excluding the NaNs. I have tryed several commands, but I haven't been able to get this to work.
Could you help me?
Thank you in advance,
Antonio :)

Answers (2)

As described in the doumentation for std, there is a flag on how to deal with 'NaN' values. If you want the standard deviation of all values, excluding the NaNs, then write
% dummy matrix
m=rand(170, 170, 27);
m(m>0.9) = NaN;
sdev = std(m, 0, 'all', 'omitnan')

4 Comments

Thank you for your answer! Unfortunatly, it doesn't seem to solve the problem. The following information appears:
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in var (line 109)
n = size(x,dim);
Error in std (line 51)
y = sqrt(var(varargin{:}));
Error in untitled2 (line 3)
sdev = std(m, 0, 'all', 'omitnan');
What release are you using? In previous releases the syntax for this function was slightly different.
The 'all' syntax was introduced in R2018b, so you'll have to linearize your array like I describe in my answer. doc for R2018a, doc for R2018b

Sign in to comment.

This syntax should work for most releases:
sdev=std(m(:),'omitnan');

Categories

Commented:

Rik
on 17 Sep 2019

Community Treasure Hunt

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

Start Hunting!