Selecting specific values from anonymous functions

1 view (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 23 Sep 2019
Edited: Stephen23 on 23 Sep 2019
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

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!