如何在 MATLAB 中将列的数据标准化为平均值?

发布于 2024-09-12 12:13:35 字数 149 浏览 2 评论 0原文

我正在尝试采用一个矩阵,并将每个单元格中的值围绕该列的平均值进行标准化。归一化是指从该列的平均值中减去每个单元格中的值,即从 Column1 中的值减去 Column1 的平均值...从 ColumnN 中的值减去 ColumnN 的平均值。我正在寻找 Matlab 中的脚本。谢谢!

I am trying to take a matrix and normalize the values in each cell around the average for that column. By normalize I mean subtract the value in each cell from the mean value in that column i.e. subtract the mean for Column1 from the values in Column1...subtract mean for ColumnN from the values in ColumnN. I am looking for script in Matlab. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

⒈起吃苦の倖褔 2024-09-19 12:13:35

您可以使用函数 mean 来获取每列的平均值,然后使用函数 bsxfun 从每列中减去该值:

M = bsxfun(@minus, M, mean(M, 1));

此外,从版本 R2016b 开始,您可以利用 MATLAB 执行 将操作数隐式扩展到算术运算的正确大小。这意味着您可以简单地执行以下操作:

M = M-mean(M, 1);

You could use the function mean to get the mean of each column, then the function bsxfun to subtract that from each column:

M = bsxfun(@minus, M, mean(M, 1));

Additionally, starting in version R2016b, you can take advantage of the fact that MATLAB will perform implicit expansion of operands to the correct size for the arithmetic operation. This means you can simply do this:

M = M-mean(M, 1);
红墙和绿瓦 2024-09-19 12:13:35

对于初学者来说,请尝试 mean 函数。向其传递一个矩阵将导致所有列被平均并返回一个行向量。

接下来,您需要减去平均值。为此,矩阵必须具有相同的大小,因此对平均行向量使用repmat。

a=rand(10);
abar=mean(a);
abar=repmat(abar,size(a,1),1);
anorm=a-abar;

或单行:

anorm=a-repmat(mean(a),size(a,1),1);

Try the mean function for starters. Passing a matrix to it will result in all the columns being averaged and returns a row vector.

Next, you need to subtract off the mean. To do that, the matrices must be the same size, so use repmat on your mean row vector.

a=rand(10);
abar=mean(a);
abar=repmat(abar,size(a,1),1);
anorm=a-abar;

or the one-liner:

anorm=a-repmat(mean(a),size(a,1),1);
无声情话 2024-09-19 12:13:35
% Assuming your matrix is in A
m = mean(A);
A_norm = A - repmat(m,size(A,1),1)
% Assuming your matrix is in A
m = mean(A);
A_norm = A - repmat(m,size(A,1),1)
倾城泪 2024-09-19 12:13:35

正如已经指出的,您将需要 mean 函数,在没有任何附加参数的情况下调用该函数时,会给出输入中每列的平均值。然后会出现一个轻微的复杂情况,因为你不能简单地减去平均值——它的维度与原始矩阵不同。

因此,请尝试以下操作:

a = magic(4)
b = a - repmat(mean(a),[size(a,1) 1]) % subtract columnwise mean from elements in a

repmat 复制均值以匹配数据维度。

As has been pointed out, you'll want the mean function, which when called without any additional arguments gives the mean of each column in the input. A slight complication then comes up because you can't simply subtract the mean -- its dimensions are different from the original matrix.

So try this:

a = magic(4)
b = a - repmat(mean(a),[size(a,1) 1]) % subtract columnwise mean from elements in a

repmat replicates the mean to match the data dimensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文