MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何为矩阵的该列中的特定值分配列均值? (https://www.labfans.com/bbs/showthread.php?t=26560)

poster 2019-12-14 20:13

如何为矩阵的该列中的特定值分配列均值?
 
我想要类似的东西:

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我想将当前列(即当前零值所在的列)的平均值分配给当前零值处的矩阵项?



[B]回答:[/B]

您可以将数组做成与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%。



[url=https://stackoverflow.com/questions/5258695]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:03

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.