Filter löschen
Filter löschen

Error while passing function to matlab's svds function

2 Ansichten (letzte 30 Tage)
christina
christina am 12 Sep. 2018
Kommentiert: christina am 12 Sep. 2018
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');

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Sep. 2018
Bearbeitet: Stephen23 am 12 Sep. 2018
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 Kommentare
Stephen23
Stephen23 am 12 Sep. 2018
Bearbeitet: Stephen23 am 12 Sep. 2018
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 am 12 Sep. 2018
Yes, that worked. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by