How can I create a piecewise inline function in MATLAB?

前端 未结 4 607
夕颜
夕颜 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:34

    Unfortunately, MATLAB doesn't have a ternary operator which would make this sort of thing easier, but to expand slightly on gnovice's approach, you could create an anonymous function like so:

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

    In general, anonymous functions are more powerful than inline function objects, and allow you to create closures etc.

提交回复
热议问题