在 lsqr MATLAB 中使用函数句柄
Using function handle in lsqr MATLAB
我想使用 lsqr 最小化 FT^-1(Ax-B)。
A 是一个巨大的稀疏矩阵,我用 3 个向量定义它:
RA(L) is the Lth nonzero of A, counting across row 1, then across row 2, and so on;
JA(L) is the column in which the Lth nonzero of A lies;
NA(I) is the number of nonzero coefficients in the Ith row of A.
我使用用户定义的嵌套函数 APROD 计算了 Ax,我试图将其作为函数句柄传递到 lsqr 中。但是,它 returns 一条错误消息:输入参数过多。
这是我的代码:
function x = PROVA
%RA, NA, JA, m are defined here;
mode=1;
x=lsqr(@APROD,B,atol,itnlim);
end
function result=APROD(x1)
%if mode=1 computes y=y+Ax
%if mode=2 computes x=x+A_(transpose)y
%A is stored in RA,JA,NA by rows
L2=0;
if mode==1
result=zeros(m,1);
for c=1:m
sum=0;
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
sum=sum+RA(L)*x1(J);
end
result(c)=result(c)+sum;
end
end
if mode==2
result=zeros(n,1);
for c=1:m
Yc=y(c);
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
result(J)=result(J)+RA(L)*Yc;
end
end
end
end
这是错误信息:
Error using iterapp (line 59)
user supplied function ==> APROD failed with the following error:
Too many input arguments.
Error in lsqr (line 190)
v = iterapp('mtimes',afun,atype,afcnstr,u,varargin{:},'transp');
Error in PROVA (line 97)
x=lsqr(@APROD,B,atol,itnlim);
我不知道我做错了什么,这是我第一次使用函数句柄,我阅读了有关它的其他问题,但我没有设法解决我的问题。感谢您的帮助!
函数句柄应采用 2 个参数:第一个是解近似向量,第二个是可以是 'notransp'
或 'transp'
:
的字符
function result=APROD(x1, mode)
L2=0;
switch mode
case 'notransp'
result = zeros(m,1);
for c=1:m
sum=0;
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
sum=sum+RA(L)*x1(J);
end;
result(c)=result(c)+sum;
end;
case 'transp'
result = zeros(n,1);
for c=1:m
Yc=y(c);
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
result(J)=result(J)+RA(L)*Yc;
end
end
end
end
我想使用 lsqr 最小化 FT^-1(Ax-B)。 A 是一个巨大的稀疏矩阵,我用 3 个向量定义它:
RA(L) is the Lth nonzero of A, counting across row 1, then across row 2, and so on;
JA(L) is the column in which the Lth nonzero of A lies;
NA(I) is the number of nonzero coefficients in the Ith row of A.
我使用用户定义的嵌套函数 APROD 计算了 Ax,我试图将其作为函数句柄传递到 lsqr 中。但是,它 returns 一条错误消息:输入参数过多。
这是我的代码:
function x = PROVA
%RA, NA, JA, m are defined here;
mode=1;
x=lsqr(@APROD,B,atol,itnlim);
end
function result=APROD(x1)
%if mode=1 computes y=y+Ax
%if mode=2 computes x=x+A_(transpose)y
%A is stored in RA,JA,NA by rows
L2=0;
if mode==1
result=zeros(m,1);
for c=1:m
sum=0;
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
sum=sum+RA(L)*x1(J);
end
result(c)=result(c)+sum;
end
end
if mode==2
result=zeros(n,1);
for c=1:m
Yc=y(c);
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
result(J)=result(J)+RA(L)*Yc;
end
end
end
end
这是错误信息:
Error using iterapp (line 59)
user supplied function ==> APROD failed with the following error:
Too many input arguments.
Error in lsqr (line 190)
v = iterapp('mtimes',afun,atype,afcnstr,u,varargin{:},'transp');
Error in PROVA (line 97)
x=lsqr(@APROD,B,atol,itnlim);
我不知道我做错了什么,这是我第一次使用函数句柄,我阅读了有关它的其他问题,但我没有设法解决我的问题。感谢您的帮助!
函数句柄应采用 2 个参数:第一个是解近似向量,第二个是可以是 'notransp'
或 'transp'
:
function result=APROD(x1, mode)
L2=0;
switch mode
case 'notransp'
result = zeros(m,1);
for c=1:m
sum=0;
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
sum=sum+RA(L)*x1(J);
end;
result(c)=result(c)+sum;
end;
case 'transp'
result = zeros(n,1);
for c=1:m
Yc=y(c);
L1=L2+1;
L2=L2+NA(c);
for L=L1:L2
J=JA(L);
result(J)=result(J)+RA(L)*Yc;
end
end
end
end