How to perform svd without using the inbuilt function?

8 ビュー (過去 30 日間)
Ashwini Subhash
Ashwini Subhash 2022 年 11 月 27 日
回答済み: Jan 2022 年 11 月 27 日
% Importing the given data file
image = importdata('data.mat');
I = double(im2gray(image));
axis off
title('Original Image');
% Performing SVD:
[U,S,V] = svd(I,'econ');
sv = diag(S);
grid on
xlabel('Rank r');
ylabel('sigma_r /sigma_1');
title('Semilog Plot');

回答 (2 件)

John D'Errico
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 日


Community Treasure Hunt

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

Start Hunting!

Translated by