Matrix with function handle: how to evaluate for a vector

Hello everyone,
Is there a way, if given a certain matrix with some function handles of n variables, to evaluate it with of vector giving values to those n variables?
Here's my exemple to make it clearer:
I have this matrix that is given has an input for a fucntion I'm coding:
J = @(x,y,z)[2*exp(2*x),0,-cos(x);-1,1,0;0,2*z,1]
And I have the vector:
X=[1;1;1] (I also tried writing it as X=[1,1,1])
And I would like to evaluate J with X in my code, but nothing I tried seems to work. Both J(X) and feval(J,X) return errors. Basically I would like to have the matrix J(1,1,1) without having to write it that way because I would like to directly take my matrix input to evaluate it with the vector input. Is there a way to do it?

 採用された回答

Matt J
Matt J 2021 年 3 月 17 日
編集済み: Matt J 2021 年 3 月 17 日

1 投票

Write J so that it has single argument syntax,
J = @(xyz) [2*exp(2*xyz(1)),0,-cos(xyz(1));-1,1,0;0,2*xyz(3),1];
X=[1;1;1];
J(X)
ans = 3×3
14.7781 0 -0.5403 -1.0000 1.0000 0 0 2.0000 1.0000

2 件のコメント

Pascale Robillard
Pascale Robillard 2021 年 3 月 18 日
Thank you! :)
Matt J
Matt J 2021 年 3 月 18 日
You're welcome, but please Accept-click the preferred answer your problem.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 17 日

1 投票

J3 = @(V)J(V(1),V(2),V(3))
However if you happen to be generating the J function using the Symbolic toolbox then see the Vars option for matlabFunction, in particular 'vars', {[x, y, z]} would cause the generated function to expect a row vector of values.

カテゴリ

質問済み:

2021 年 3 月 17 日

コメント済み:

2021 年 3 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by