f = 
How to define a piecewise anonymous function
Show older comments
Hello everyone,
the example code
syms x
continuous_function = x^2+x;
matlabFunction(continuous_function,'Vars',x)
gives me the anonymous function
@(x)x+x.^2
I would like a similar result for piecewise functions. However, the code
syms x
piecewise_function = piecewise( 0<x<1, x, 'otherwiseVal', 0 );
matlabFunction(piecewise_function,'Vars',x)
does not work.
Thanks for any help!
Accepted Answer
More Answers (1)
Walter Roberson
on 16 May 2020
0 votes
If you use matlabFunction with 'file' option, then it will convert piecewise() into if/else in the generated code.
Note: because it uses if/else instead of logical indexing, the generated code will not be vectorized on that variable.
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

