How to perform svd without using the inbuilt function?
8 ビュー (過去 30 日間)
% Importing the given data file
image = importdata('data.mat');
I = double(im2gray(image));
% Performing SVD:
[U,S,V] = svd(I,'econ');
sv = diag(S);
回答 (2 件)
John D'Errico 2022 年 11 月 27 日
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 2022 年 11 月 27 日
You can find working solutions in the FileExchange: