Reputation: 29
I am researching the above topic and attempting to play about with SVD code in Matlab. I was wondering can anyone explain what the following line of code does?
Sh(logical(eye(size(Sh)))) = Sh_diag;
I can understand the rest, it's just this line of code that is puzzling me. I have tried running in debugger but still can't understand it! I believe eye
returns an identity matrix and logical
converts Sh
to be 1 and 0's?? But I cant figure out how they work together, especially with size
of Sh
? Thank you in advance.
%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;%%%????%%
Upvotes: 1
Views: 359
Reputation: 5586
size(Sh)
returns the dimensions of the matrix Sh
.
eye(size(Sh))
creates an identity matrix with the same dimensions as Sh
.
logical(eye(size(Sh)))
converts the elements of the identity matrix to logical values.
Sh(...)
is selecting a submatrix of Sh
using logical indexing. Here it looks like it's just getting the diagonal elements of Sh
.
Sh(...) = Sh_diag
is replacing the aforementioned submatrix with Sh_diag
.
So in all, this is selecting the diagonal elements of Sh
and replacing them with the values in Sh_diag
.
Upvotes: 3