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
  1 件のコメント
Jonathan Rabe
Jonathan Rabe 2019 年 9 月 23 日
This works perfectly. Thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by