我想要类似的东西:
Mu = mean(X); % column-wise means X(X == 0) = Mu(current_column); % assign the mean value of the current column % to the zero-element 但是,如何告诉MATLAB我想将当前列(即当前零值所在的列)的平均值分配给当前零值处的矩阵项?
回答:
您可以将数组做成与X相同的形状,其中包含按列表示:
means = repmat(mean(X), [size(X,1) 1]); X(X==0) = means(X==0); [编辑添加...]
或者,如果显式扩展数组令您不满意,则可以这样做:
X = bsxfun(@(x,y)(x+(x==0)*y), X, mean(X)); 对于我的口味来说,这有点“聪明”,但是在我测试的单个案例(1000x1000数组,其中大约10%为零)下,速度似乎快了25%。
更多&回答...