MATLAB: Standard deviation ignoring 0s in matrix

deviationstandard

Hi all, I have a 295×34 matrix called Absy. I want to take the standard deviation of all the columns, however some columns contain many zeros. I want to ignore the zeros and take the standard deviation (as zeros are only their due to some columns being different size).
Thank you.

Best Answer

One easy way would be to convert all zeros to NaN, and then use the nanstd function to ignore the NaNs in the std calculation.
Absy(Absy==0)=NaN;
s = nanstd(Absy,[],1); %column by column std deviation