Error while passing function to matlab's svds function

3 ビュー (過去 30 日間)
christina
christina 2018 年 9 月 12 日
コメント済み: christina 2018 年 9 月 12 日
I am getting following error when I run my code. Can somebody please tell me what am I doing wrong here and how to fix it? I have a function that does the matrix vector multiplication and I want to pass it as a handle to the svd function to calculate the singular values of the matrix.
Error using @(x)ifft(bsxfun(@times,fft(x),m))
Too many input arguments.
Error in svds>LanczosBD (line 631)
u = Afun(v,f1);
Error in svds (line 166)
[U,S,V,flag] = LanczosBD(Afun,m,n,f1,f2,k,v,InnerOpts,randStr);
Code:
L= 2^18;
T = 500; % Number of spikes 40
x = zeros(L,1);
q = randperm(L);
x(q(1:T)) = 2*sign(randn(T,1));
m = rand(L,1);
h = @(x) ifft(bsxfun(@times,fft(x),m)); % Function handle
y = h(x);
s = svds(h,[L L],6,'largest');

採用された回答

Stephen23
Stephen23 2018 年 9 月 12 日
編集済み: Stephen23 2018 年 9 月 12 日
The svds help shows that the first input can either be a matrix, or a function handle:
" s = svds(Afun,n,...) specifies a function handle Afun instead of a matrix. The second input n gives the size of matrix A used in Afun."
You will notice that in the help page, the input Afun is a link. When you click on that link it scrolls down the page to the section that specifies how the function must be defined. There it states:
"The function Afun must satisfy these conditions:"
  • " Afun(x,'notransp') accepts a vector x and returns the product A*x."
  • " Afun(x,'transp') accepts a vector x and returns the product A'*x."
Your function does not support either of those two conditions (in fact your function does not support any second argument at all, thus the error message). You will need to write your function to accept a second input argument and return appropriate values, as per the documentation.
  3 件のコメント
Stephen23
Stephen23 2018 年 9 月 12 日
編集済み: Stephen23 2018 年 9 月 12 日
I suspect that you meant to do this:
s = svds(@(x,tflag) afun(x,tflag,m), [L,L], 10);
^^^^^^^ only two input arguments, as per the documentation.
The anonymous function should only have two input arguments, not three as you had it defined. The variable m is taken from the workspace when the anonymous function is defined.
christina
christina 2018 年 9 月 12 日
Yes, that worked. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by