standard deviation of non-zero elements of columns of a matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
osminbas
el 1 de Mzo. de 2012
Respondida: David Freese
el 11 de Oct. de 2013
Hello,
How can I find the standard deviation of each column of a matrix ignoring the zero values? I must add that I do not have the stats package so I cannot use nanstd! Thank you!
0 comentarios
Respuesta aceptada
the cyclist
el 1 de Mzo. de 2012
Here is a straightforward way.
% Some input data
x = rand(7,6);
x(1,1) = 0; % Put in a couple zeros by hand
x(6,5) = 0;
% Preallocate the array that will hold the standard deviations
sd = zeros(1,6);
% Calculate and store the standard deviations of the non-zero elements.
for nc = 1:6
indexToNonZero = x(:,nc)~=0;
sd(nc) = std(x(indexToNonZero,nc));
end
Más respuestas (2)
Walter Roberson
el 1 de Mzo. de 2012
There is a MATLAB File Exchange contribution to provide these functions.
0 comentarios
David Freese
el 11 de Oct. de 2013
A way to go about it using just matrix operations:
% Some input data
x = rand(7,6);
x(1,1) = 0; % Put in a couple zeros by hand
x(6,5) = 0;
% Calculate and store the standard deviations of the non-zero elements.
% using the identity std^2 = Var(x) = mean(x.^2,2) - mean(x,2).^2
avg = sum(x,2)./sum(x~=0,2);
sd = sqrt(sum(x.^2,2)./sum(x~=0,2) - avg.^2);
% Zero out any of the columns that were all zeros
sd(isnan(sd)) = 0;
0 comentarios
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!