How to perform svd without using the inbuilt function?

26 views (last 30 days)
% Importing the given data file
image = importdata('data.mat');
I = double(im2gray(image));
figure(1);
imagesc(I);
axis off
title('Original Image');
% Performing SVD:
[U,S,V] = svd(I,'econ');
sv = diag(S);
figure(2);
semilogy(sv);
grid on
xlim([-50,1050])
xlabel('Rank r');
ylabel('sigma_r /sigma_1');
title('Semilog Plot');

Answers (2)

John D'Errico
John D'Errico on 27 Nov 2022
Why? That is, why is SVD not acceptable?
Yes, you could write a complete code to compute the SVD, without using a call to SVD. That would be terribly slow code, because it would be written in MATLAB, not in a languagle like C or Fortran. MATLAB will do the call using a call to LAPACK routines, so they will be fast and reliable. You seriously don't want to write an SVD yourself.
But, yes, you could spend the time to call the same external libraries, using a mex interface. That seemes silly to me, to write the code to do exactly what MATLAB already does for you, thus provide an interface to the same library.
Finally, you COULD use eig. This will effectively square the singular values, and so you will lose the ability to resolve ALL of the small singular values. Anything smaller than a rough factor of 1e-8 times the largest singular value will now be numerical garbage. You don't want that.

Jan
Jan on 27 Nov 2022

Categories

Find more on Eigenvalues in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!