Unexpected results using SVD to separate components that make up a function
As preliminary to a larger project, I was trying out the SVD function with a test matrix made up by adding three 2D gaussians p1, p2, p3. I was hoping the SVD would be able to separate the function into these three components, and they would have the largest singular values in the S matrix.
I then tried to remake these components by setting everything except one singuar value in the S matrix to zero and calculating U*S_new*V’.
This does not make anything resembling my original components. Have I misunderstood how SVD should work, or have I misunderstood how separating the components should work?
I have attached my code:
% SVD test
clear all;
[X,Y] = meshgrid(-2:.1:4);
mu1 = [1 2];
Sigma1 = [5 3;3 5];
p1 = mvnpdf([X(:) Y(:)],mu1,Sigma1);
p1 = reshape(p1,size(X));
mu2 = [0 1];
Sigma2 = [3 0;0 1];
p2 = mvnpdf([X(:) Y(:)],mu2,Sigma2);
p2 = reshape(p2,size(X));
mu3 = [3 0];
Sigma3 = [2 1;1 2];
p3 = mvnpdf([X(:) Y(:)],mu3,Sigma3);
p3 = reshape(p3,size(X));
p = p1+p2+p3;
[U, S, V] = svd(p, "econ");
p_remade = U*S*V’;
figure;
subplot(3,1,1); imagesc(p1)
subplot(3,1,2); imagesc(p2)
subplot(3,1,3); imagesc(p3)
sgtitle("original components making p")
figure;
subplot(2,1,1); imagesc(p)
title("original p")
subplot(2,1,2); imagesc(p_remade)
title("SVD p")
num_comp = 3;
Si = zeros(size(S));
figure;
comp = zeros(length(S),length(S),num_comp);
for i = 1:num_comp
Si(i,i) = S(i,i);
comp(:,:,i) = U*Si*V’;
subplot(num_comp,1,i); imagesc(squeeze(comp(:,:,i)));
endAs preliminary to a larger project, I was trying out the SVD function with a test matrix made up by adding three 2D gaussians p1, p2, p3. I was hoping the SVD would be able to separate the function into these three components, and they would have the largest singular values in the S matrix.
I then tried to remake these components by setting everything except one singuar value in the S matrix to zero and calculating U*S_new*V’.
This does not make anything resembling my original components. Have I misunderstood how SVD should work, or have I misunderstood how separating the components should work?
I have attached my code:
% SVD test
clear all;
[X,Y] = meshgrid(-2:.1:4);
mu1 = [1 2];
Sigma1 = [5 3;3 5];
p1 = mvnpdf([X(:) Y(:)],mu1,Sigma1);
p1 = reshape(p1,size(X));
mu2 = [0 1];
Sigma2 = [3 0;0 1];
p2 = mvnpdf([X(:) Y(:)],mu2,Sigma2);
p2 = reshape(p2,size(X));
mu3 = [3 0];
Sigma3 = [2 1;1 2];
p3 = mvnpdf([X(:) Y(:)],mu3,Sigma3);
p3 = reshape(p3,size(X));
p = p1+p2+p3;
[U, S, V] = svd(p, "econ");
p_remade = U*S*V’;
figure;
subplot(3,1,1); imagesc(p1)
subplot(3,1,2); imagesc(p2)
subplot(3,1,3); imagesc(p3)
sgtitle("original components making p")
figure;
subplot(2,1,1); imagesc(p)
title("original p")
subplot(2,1,2); imagesc(p_remade)
title("SVD p")
num_comp = 3;
Si = zeros(size(S));
figure;
comp = zeros(length(S),length(S),num_comp);
for i = 1:num_comp
Si(i,i) = S(i,i);
comp(:,:,i) = U*Si*V’;
subplot(num_comp,1,i); imagesc(squeeze(comp(:,:,i)));
end As preliminary to a larger project, I was trying out the SVD function with a test matrix made up by adding three 2D gaussians p1, p2, p3. I was hoping the SVD would be able to separate the function into these three components, and they would have the largest singular values in the S matrix.
I then tried to remake these components by setting everything except one singuar value in the S matrix to zero and calculating U*S_new*V’.
This does not make anything resembling my original components. Have I misunderstood how SVD should work, or have I misunderstood how separating the components should work?
I have attached my code:
% SVD test
clear all;
[X,Y] = meshgrid(-2:.1:4);
mu1 = [1 2];
Sigma1 = [5 3;3 5];
p1 = mvnpdf([X(:) Y(:)],mu1,Sigma1);
p1 = reshape(p1,size(X));
mu2 = [0 1];
Sigma2 = [3 0;0 1];
p2 = mvnpdf([X(:) Y(:)],mu2,Sigma2);
p2 = reshape(p2,size(X));
mu3 = [3 0];
Sigma3 = [2 1;1 2];
p3 = mvnpdf([X(:) Y(:)],mu3,Sigma3);
p3 = reshape(p3,size(X));
p = p1+p2+p3;
[U, S, V] = svd(p, "econ");
p_remade = U*S*V’;
figure;
subplot(3,1,1); imagesc(p1)
subplot(3,1,2); imagesc(p2)
subplot(3,1,3); imagesc(p3)
sgtitle("original components making p")
figure;
subplot(2,1,1); imagesc(p)
title("original p")
subplot(2,1,2); imagesc(p_remade)
title("SVD p")
num_comp = 3;
Si = zeros(size(S));
figure;
comp = zeros(length(S),length(S),num_comp);
for i = 1:num_comp
Si(i,i) = S(i,i);
comp(:,:,i) = U*Si*V’;
subplot(num_comp,1,i); imagesc(squeeze(comp(:,:,i)));
end svd, linear algebra, matlab MATLAB Answers — New Questions