奇异值分解 Matlab

Singular Values Decomposition Matlab

我正在研究上述主题并尝试在 Matlab 中使用 SVD 代码。我想知道任何人都可以解释以下代码行的作用吗?

Sh(logical(eye(size(Sh)))) = Sh_diag;

其他的我都能看懂,就是这行代码让我费解。我在调试器中尝试了 运行 但仍然无法理解!我相信 eye returns 是一个单位矩阵并且 logicalSh 转换为 1 和 0?但我无法弄清楚它们是如何协同工作的,尤其是 size of Sh?提前谢谢你。

%Apply SVD to img1
[Uh Sh Vh] = svd(img1);

% Apply SVD to img2
[Uw Sw Vw] = svd(img2);

% Replace singular values of the img1 with the
% singular values of the img2
Sh_diag = diag(Sh);
Sw_diag = diag(Sw);


if (length(img2) >= 256)
    Sh_diag(1:length(Sh), :) = Sw_diag(1:length(Sh), :);
elseif(length(hidden_img) < 256)
    Sh_diag(1:length(img2), :) = Sw_diag(1:length(img2), :);
end
Sh(logical(eye(size(Sh)))) = Sh_diag;%%%????%%

size(Sh) returns 矩阵的维度 Sh.

eye(size(Sh)) 创建一个与 Sh.

具有相同维度的单位矩阵

logical(eye(size(Sh))) 将单位矩阵的元素转换为逻辑值。

Sh(...) 正在使用 logical indexing 选择 Sh 的子矩阵。这里看起来只是获取 Sh.

的对角线元素

Sh(...) = Sh_diag 将上述子矩阵替换为 Sh_diag

所以总而言之,这是选择 Sh 的对角线元素并用 Sh_diag 中的值替换它们。