Selecting specific values from anonymous functions

조회 수: 1 (최근 30일)
Jonathan Rabe
Jonathan Rabe 2019년 9월 23일
댓글: Jonathan Rabe 2019년 9월 23일
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.

채택된 답변

Stephen23
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 CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by