设矩阵 ,对其2-范数化,即
其中
那么, 可以用伪代码 来实现。
clear
clc
% (1). Loop method
x = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
[m, n] = size(x);
xBar = zeros(m, n);
for jj = 1 : n
xBar(:, jj) = x(:, jj) / norm(x(:, jj), 2);
end
% (2). Vectorization method
xBar = x * diag(1 ./ sqrt(diag(x'*x)));
Output result:
>> xBar =
0.0776 0.1367 0.1826
0.3105 0.3418 0.3651
0.5433 0.5469 0.5477
0.7762 0.7519 0.7303
计算矩阵各列之间的欧式距离向量化方法
设矩阵 ,计算 . 直接计算:
写成矩阵形式:
如果令:
则最终结果可表示成:.
Matlab代码实现
clear
clc
% (1). Loop method
x = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12];
nDim = size(x, 2);
k = zeros(nDim, nDim);
for ii = 1 : nDim
xi = x(:, ii);
for jj = 1 : nDim
xj = x(:, jj);
k(ii, jj) = sqrt((xi - xj).' * (xi-xj));
end % end for
end % end for
% (2). Vectorization method
x2 = x' * x;
dx = diag(x2) * ones(size(diag(x2)))';
k = sqrt(dx + dx' - 2 * x2);
Output result:
>> k =
0 2 4
2 0 2
4 2 0
标签:nDim,xj,diag,矩阵,jj,量化,范数
From: https://www.cnblogs.com/qpwz/p/16775694.html