create a function handle extracting pieces from a matrix function handle

12 ビュー (過去 30 日間)
Sergio Codeluppi
Sergio Codeluppi 2013 年 6 月 4 日
I created a Matrix function using function handle @ like
A=@(x) [1 2 x; 2*x x.^2 3]; %it's an example
i need to create another function extracted from the previous one
B=@(x) A(x) without the second column
therefore size(A)=2x3 and size(B)= 2x2;
if it wasn't for the handle: B=A(:,[1 3])
ty for your help

採用された回答

Kye Taylor
Kye Taylor 2013 年 6 月 4 日
編集済み: Kye Taylor 2013 年 6 月 4 日
Starting from your example
A = @(x) [1 2 x; 2*x x.^2 3];
you probably realized that this command is not allowed
B = @(x)[A(x)(:,1),A(x)(:,3)]
However, you can use this command:
B = @(x)[A(x)*[1;0;0],A(x)*[0;0;1]]
  1 件のコメント
Sergio Codeluppi
Sergio Codeluppi 2013 年 6 月 4 日
ty for your help, i actually didn't think of this :)

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 6 月 4 日
B=@(x) subsref(A(x), struct('type', '()', 'subs', {':', [1 3]}));
Note: if the requirement is "all of whatever is there, except column 2", rather than "select only column 1 and column 3", then in order to avoid evaluating A(x) twice, one would have to add a helper anonymous function (because a subscript of 'end' is not allowed at the subsref level and must be converted to numeric form.)
  1 件のコメント
Sergio Codeluppi
Sergio Codeluppi 2013 年 6 月 4 日
i actually don't know command subsref, but tried your way and i get this
Error using subsref The "subs" field for the subscript argument to SUBSREF and SUBSASGN must be a cell or character array.
ty anyway for your help!

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

カテゴリ

Help Center および File ExchangeTime Series についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by