How can I create a piecewise inline function in MATLAB?

前端 未结 4 612
夕颜
夕颜 2021-01-05 07:04

I have a function in MATLAB which takes another function as an argument. I would like to somehow define a piecewise inline function that can be passed in. Is this somehow po

4条回答
  •  执念已碎
    2021-01-05 07:50

    If you really want to make an inline function (as opposed to an anonymous function), then the following would probably be the simplest way:

    f = inline('2.*(x <= 0.5)-1');
    

    However, as pointed out in the other answers, anonymous functions are more commonly used and are more efficient:

    f = @(x) (2.*(x <= 0.5)-1);
    

提交回复
热议问题