Selecting specific values from anonymous functions
1 次查看(过去 30 天)
显示 更早的评论
Hi, for my anonymous function as below, I would like to have it as one single function. Something like this:
g = @(th) TsMat(thetas)(3, :) + R.*FsMat(thetas)(1,:);
But so far I only seem to be getting this right: (which makes them no longer functions)
TsMat = Ts(thetas);
FsMat = Fs(thetas);
g = @(th) TsMat(3, :) + R.*FsMat(1,:);
Any help would be much appreciated! Thank you in advance.
0 个评论
采纳的回答
Stephen23
2019-9-23
编辑:Stephen23
2019-9-23
You could call subsref directly, e.g.:
subsref(TsMat(thetas),substruct('()',{3,':'}))
and similarly for the other function calls. But this is a bit bulky...
A simpler solution is to write a small indexing function:
fun = @(M,R)M(R,:);
fun(TsMat(thetas),3)
You could even generalize it to work with any number of indices, e.g.:
>> fun = @(A,varargin)A(varargin{:});
>> M = rand(5,4);
>> fun(sqrt(M),3,':')
ans =
0.92148 0.62628 0.52623 0.56312
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!