use of svds with function handles

조회 수: 2 (최근 30일)
min lee
min lee 2021년 11월 5일
댓글: min lee 2021년 11월 6일
I want to do singular value decomposition of a matrix A, which is very big but simply structured. Specifically, it is a rank-2 matrix plus a diagonal matrix. I just need the first few singular values. So I want to call "svds", and I do not want to construct A explicitly as a matrix, as that is too costy in memory. The idea is to write a function to perform the multiplication x---> A*x
This is the nested function for the multiplication:
function y = fun_A_times_x(x)
y = sum(phi2.*x)*kphi1 + sum(phi1.*x)*kphi2...
+ sum(kphi1.*x)*phi2 + sum(kphi2.*x)*phi1...
+ diagonal.*x - E*sum(phi1.*x)*phi2 - E* sum(phi2.*x)*phi1;
end
How should I write the svds part?
myfun = @fun_A_times_x ;
[UU,SS,VV] = svds(@(x) myfun, [dim dim], 2);
This does not work.
I still do not understand the mechanism of the function handle. Can anyone help me out? My matlab is R2017a.

채택된 답변

Steven Lord
Steven Lord 2021년 11월 5일
When in doubt, check the documentation page to see if there's an example you can adapt. In this case there is, "Largest Singular Values Using Function Handle". You can also look at the description of the Afun input argument on that page to determine what behavior your value for that input is expected to satisfy.
Your function needs to accept two input arguments: a vector and a flag that is either 'transp' or 'notransp'. The flag value svds passes into your function determines what it needs to compute and return to svds.
In the code you posted, once you modify fun_A_times_x to accept two inputs and return the appropriate result based on the flag value if fun_A_times_x requires only the two input arguments with which svds will call it you can just pass a function handle to it as the first input argument.
[UU,SS,VV] = svds(@fun_A_times_x, [dim dim], 2);
In the example in the documentation B, C, and n are being passed in as additional parameters using the anonymous function approach described on this documentation page. Since you mentioned fun_A_times_x is a nested function it seems you're using the nested function approach also described on that documentation page and so you won't need those additional parameters. Just modify your fun_A_times_x so it works with the two-input syntax and it should work.
  댓글 수: 1
min lee
min lee 2021년 11월 6일
Yes, it now works! Although I still do not understand the mechanism of function handles, but now it works.
I did not know for svd, we need both A*x and A'*x. For eig, we need only A*x.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Christine Tobler
Christine Tobler 2021년 11월 5일
The svds function needs to be able to apply both A*x and A'*x, so your function handle should accept a second input which is either 'transp' or 'notransp' and then compute either A*x or A'*x accordingly.
  댓글 수: 1
min lee
min lee 2021년 11월 6일
Yes, this is the point, for svd, we need both A*x and A'*x. I did not know this before.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by