Standard deviation ignoring 0s in matrix

조회 수: 24 (최근 30일)
Mate 2u
Mate 2u 2012년 7월 24일
Hi all, I have a 295x34 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.

채택된 답변

Matt Kindig
Matt Kindig 2012년 7월 24일
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
  댓글 수: 1
Mate 2u
Mate 2u 2012년 7월 24일
Is their a way to do this with kurtosis also?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 7월 24일
편집: Jan 2012년 7월 24일
x = rand(295, 34);
x(rand(size(x)) < 0.1) = 0;
index = (x ~= 0);
sumnz = sum(index, 1);
meanx = sum(x, 1) ./ sumnz;
xm = bsxfun(@minus, x, meanx);
xm(~index) = 0;
s = sqrt(sum(xm .* xm, 1) ./ (sumnz - 1));
[NOT TESTED]

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by