Error while passing function to matlab's svds function

조회 수: 2 (최근 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 CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by