How to return a mean value with NaN values in the matrix?

3 次查看(过去 30 天)
I have written a piece of code which states if a number is > 1 or < 0.05 then it should return a NaN value:
if JumpHeightImpulse(i) > 1
JumpHeightImpulse(i) = 0;
end
if JumpHeightImpulse(i) < 0.05
JumpHeightImpulse(i) = 0;
end
end
And then I have tried to find the mean and standard deviation of my values using nanmean and nanstd:
for i = 1:length(NoS);
for j = 1:4
A = CombinedData(CombinedData(:,2)==i,:);
B = A(A(:,3)==j,:);
mean(i,j) = nanmean(B(:,5));
stdev(i,j) = nanstd(B(:,5));
end
end
However when I input this it returns NaN values for the mean and st dev. Any ideas as to why this is? Thanks
  2 个评论
Iain
Iain 2013-5-20
编辑:Image Analyst 2013-5-20
We need more of the code. But you could try:
JumpHeightImpulse(JumpHeightImpulse > 1) = NaN;
JumpHeightImpulse(JumpHeightImpulse < 0.05) = NaN;
mean_vals = nanmean(JumpHeightImpulse);
std_vals = nanstd(JumpHeightImpulse);

请先登录,再进行评论。

回答(2 个)

Iain
Iain 2013-5-21
We need more of the code. But you could try:
JumpHeightImpulse(JumpHeightImpulse > 1) = NaN; JumpHeightImpulse(JumpHeightImpulse < 0.05) = NaN; mean_vals = nanmean(JumpHeightImpulse); std_vals = nanstd(JumpHeightImpulse);

Andrei Bobrov
Andrei Bobrov 2013-5-21
编辑:Andrei Bobrov 2013-5-21
% 1)
JumpHeightImpulse(JumpHeightImpulse < .5 | JumpHeightImpulse > 1) = nan;
% 2) CD - your CombinedData
m = length(NoS);
n = 4;
sv = CD(all(bsxfun(@le,CD(:,2:3),[m,n]),2) & CD(:,2) > 0,[2,3,5]);
your_mean = accumarray(sv(:,1:2),sv(:,3),[m, n],@nanmean,nan);
your_std = accumarray(sv(:,1:2),sv(:,3),[m, n],@nanstd,nan);

类别

Help CenterFile Exchange 中查找有关 Earth and Planetary Science 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by