矩阵的平均值> x

时间:2013-09-24 07:28:50

标签: matlab matrix mean large-data

我需要计算矩阵的均值,但仅限于那些大于指定数字的值。现在我有了这个想法:

Media = mean(W_tot_migl,2);

H = W_tot_migl;
H(H<LimiteInferiore) = nan;
Media_b = nanmean(H,2);

有没有办法避免创建另一个矩阵H = W_tot_migl

2 个答案:

答案 0 :(得分:1)

对于矩阵H和阈值T,您可以使用:

M = nanmean(H+0./(H>T),2);

在你的情况下:

Media_b = nanmean(W_tot_migl+0./(W_tot_migl>LimiteInferiore),2);

或者自己计算:

M = sum(H.*(H>T),2)./sum(H>T,2);

对于你的情况:

Media_b = sum(W_tot_migl.*(W_tot_migl>LimiteInferiore),2)./sum(W_tot_migl>LimiteInferiore,2);

请注意,如果一行中没有大于阈值的值,则两个方法都会返回NaN

但是,通常您可以使用accumarray来完成此操作:

H2 = H>T;
[I,~] = find(H2);
M = accumarray(I, H(H2), [size(H,1) 1], @mean);

如果行中没有大于阈值的元素,则此方法返回零。

答案 1 :(得分:0)

从你想要的评论来看:

H = magic(5); %Suppose this is your matrix
LimiteInferiore = 23; %And suppose this is your treshold

Media_b= NaN(size(H,1),1); %Or perhaps zeros, whatever you like to show by default

idx = any(H > treshold,2);
Media_b(idx) = mean(H(H>treshold),2);

如果存在,它会给你rowmean,否则给你NaN。我相信你不需要NaN的意思,因为NaN值不会评估为>treshold