MATLAB中向量数组的向量范数 [英] Vector norm of an array of vectors in MATLAB
问题描述
在MATLAB的矩阵上调用 norm
时,它会返回所谓的矩阵范数"(标量值),而不是向量范数的数组.有没有什么方法可以在不循环的情况下获得矩阵中每个向量的范数,而又不利用MATLAB的向量化功能呢?
When calling norm
on a matrix in MATLAB, it returns what's known as a "matrix norm" (a scalar value), instead of an array of vector norms. Is there any way to obtain the norm of each vector in a matrix without looping and taking advantage of MATLAB's vectorization?
推荐答案
您可以使用 SUM 和
You can compute the norm of each column or row of a matrix yourself by using element-wise arithmetic operators and functions defined to operate over given matrix dimensions (like SUM and MAX). Here's how you could compute some column-wise norms for a matrix M
:
twoNorm = sqrt(sum(abs(M).^2,1)); %# The two-norm of each column
pNorm = sum(abs(M).^p,1).^(1/p); %# The p-norm of each column (define p first)
infNorm = max(M,[],1); %# The infinity norm (max value) of each column
通过将维度参数从...,1
更改为...,2
,可以轻松地使这些规范适用于行而不是列.
These norms can easily be made to operate on the rows instead of the columns by changing the dimension arguments from ...,1
to ...,2
.
这篇关于MATLAB中向量数组的向量范数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!