Selecting specific values from anonymous functions

1 visualización (últimos 30 días)
Jonathan Rabe
Jonathan Rabe el 23 de Sept. de 2019
Comentada: Jonathan Rabe el 23 de Sept. de 2019
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.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Sept. de 2019
Editada: Stephen23 el 23 de Sept. de 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

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by