对矩阵行进行归一化,以使它们的范数等于1(MATLAB) [英] Normalizing rows of matrix, so that their norm is equal to 1 (MATLAB)

查看:805
本文介绍了对矩阵行进行归一化,以使它们的范数等于1(MATLAB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题-我的矩阵A大小为16x22440.

I have a following problem - I have a matrix A of size 16x22440.

我需要做的是规范化此矩阵的每一行,以使它们的范数等于1(for n=1:16 norm(A(n,:))==1)

What I need to do is to normalize each row of this matrix, so that the norm of each of them is equal to 1 (for n=1:16 norm(A(n,:))==1)

如何在matlab中实现?

How can I achieve that in matlab?

此矩阵中的每一行都是由160x140图像创建的向量,因此必须单独考虑.需要对这些值进行归一化以创建本征面矩阵.

Each row in this matrix is a vector created of an 160x140 image and thus must be considered separately. The values need to be normalised to create an eigenfaces matrix.

推荐答案

您安装的Matlab是否包含神经网络工具箱?如果是这样,请尝试 normr :

Does your install of Matlab include the Neural Network Toolbox? If so, then try normr:

nA = normr(A);

否则, @Shai的解决方案很好,除了它不会处理无限或NaN输入之外-这很多之后更安全地检查未定义的规范案例:

Otherwise, @Shai's solution is good except that it won't handle infinite or NaN inputs – it's much safer to check undefined norm cases afterwards:

nA = bsxfun(@rdivide,A,sqrt(sum(A.^2,2)));
nA(~isfinite(nA)) = 1; % Use 0 to match output of @Shai's solution, Matlab's norm()

请注意,对零长度(所有零分量)或无限长向量(一个或多个分量+Inf-Inf)或一个具有NaN分量的归一化的定义不是很明确.上面的解决方案会返回所有结果,就像Matlab的normr函数一样. Matlab的 norm 函数表现出不同的行为.您可能希望指定不同的行为,例如警告或错误,全零,NaN,按向量长度缩放的分量等.此线程在某种程度上讨论零长度向量的问题:

Note that normalizing of a zero length (all zero components) or infinite length vector (one or more components +Inf or -Inf) or one with a NaN component is not really well-defined. The solution above returns all ones, just as does Matlab's normr function. Matlab's norm function, however, exhibits different behavior. You may wish to specify a different behavior, e.g., a warning or an error, all zeros, NaNs, components scaled by the vector length, etc. This thread discusses the issue for zero-length vectors to some extent: How do you normalize a zero vector?.

这篇关于对矩阵行进行归一化,以使它们的范数等于1(MATLAB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆