Function handle in lsqr
2 次查看(过去 30 天)
显示 更早的评论
https://www.mathworks.com/help/matlab/ref/lsqr.html
In the above page, there is code: function y = afun(x,transp_flag) if strcmp(transp_flag,'transp') % y = A'*x y = 4 * x; y(1:n-1) = y(1:n-1) - 2 * x(2:n); y(2:n) = y(2:n) - x(1:n-1); elseif strcmp(transp_flag,'notransp') % y = A*x y = 4 * x; y(2:n) = y(2:n) - 2 * x(1:n-1); y(1:n-1) = y(1:n-1) - x(2:n); end end
Could anybody please kindly tell me what is the purpose of strcmp(transp_flag,'transp')? Why there have to be two kinds of transp_flag?
0 个评论
采纳的回答
Steven Lord
2018-7-22
The first paragraph in the Description section on that documentation page states:
"You can specify A as a function handle, afun, such that afun(x,'notransp') returns A*x and afun(x,'transp') returns A'*x."
By writing a function you may be able to solve systems where explicitly creating and storing A would require a lot of time and/or memory, but A has a structure that allows you to compute its product with a vector more efficiently without needing to build A. As an extreme case, if A was eye(1e6) that would require a lot of memory to store, but A*x and A'*x are each just x.
Why does lsqr require both those products? See page 44 as well as step 3 of the algorithm on page 50 of this paper which is the second reference on the lsqr documentation page.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!