필터 지우기
필터 지우기

Stop matlabFunction converting operators to element-wise operators

조회 수: 1 (최근 30일)
Fredrik Smith
Fredrik Smith 2021년 10월 5일
답변: Matt J 2021년 10월 5일
How do I stop matlabFunction converting operators to element-wise operators when forming a function handle from a symbolic expression? For example with mtimes and times, or mpower and power.
syms x y;
fh = matlabFunction(mpower(x, y))
fh = function_handle with value:
@(x,y)x.^y
This is particularly annoying when wanting to use the function handle on classes which have the operator defined, but not the element-wise operator. For example, a transfer function:
% this works
tf('s')^3
ans = s^3 Continuous-time transfer function.
% this does not
fh(tf('s'), 3)
Operator '.^' is not supported for operands of type 'tf'.

Error in symengine>@(x,y)x.^y
This also has become a problem using the 'realp' and 'genmat' classes. I have had to define the element-wise operators in the classes myself, but surely this cannot be the optimal solution.

답변 (1개)

Matt J
Matt J 2021년 10월 5일
Perhaps as follows?
syms x y;
str = func2str( matlabFunction(mpower(x, y)) );
str(str=='.')='';
fh=str2func(str)
fh = function_handle with value:
@(x,y)x^y

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by