How to use conditions in an anonymous function

The function can be defined as @(x) x^.2 (for example)

But in case we have a function that takes a different presentation at different intervals, for example: if abs(x)<3 fun = x^.2 else 0

How can we use the same path (I mean using @(x) ) to define such a function.

+6
source share
4 answers

There are several ways to do this.

Multiply by false:

 g = @(x) (abs(x)<3) .* x.^2 

or define the correct function (actually BEST):

 function y = g(x) y = zeros(size(x), class(x)); inds = abs(x)<3; y(inds) = x(inds).^2; end 

or do the randomly-ugly-ineffective but fun thing and use inline-if :

 iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}(); g = @(x) iff( ... abs(x)<3, x.^2, ... true, 0); 
+16
source

As mentioned in @Gunther's comments above, a common function to handle simple conditions to simulate a tertiary operator ?: can be created to bypass the restriction that Matlab does not allow conventions inside anon functions (unless you think this messy-ugly-inefficient, but fun built-in version :).

The solution was presented at this link (and, possibly, in several other places, but SO returned to my Google search, so I thought it would be nice to add here). http://www.mathworks.co.jp/matlabcentral/newsreader/view_thread/158054

define a function: iff or ifelse and add it to the Matlab path.

 function result = ifelse(condition,trueResult,falseResult) error(nargchk(3,3,nargin)); % check correct number of input args if condition result = trueResult; else result = falseResult; end 

then use like this

 predict = arrayfun(@(x) ifelse(x>=0.5,1,0), inputData); 

In the case of OP, something like this can be used

 arrayfun(@(x) ifelse(abs(x)<3,x^.2,0), data) 
0
source

Here is what I came up with. I have a cases.m function with the following definition:

 function [ val ] = cases( table ) [rows,~] = size(table); for i = drange(1:rows) condition = table{i,1}; if (ischar(condition) && strcmpi(condition,'else')) || feval(condition) val = feval(table{rows,2}); return end end val={}; end 

The cases function accepts an array of cells with two columns. Each element is a function with zero arguments. For each row, it takes the first element, and if it is an else string or a function that returns the true value, the second element is called and its value is returned. If the string does not match, an empty cell is returned. Elements are null functions instead of values, so unnecessary cases are not evaluated.

Then I can write case expressions as follows:

 w=arrayfun(@(j) cases({ ... @() (j==0 || j==n) @() (-1)^j/2; ... 'else' @() (-1)^j }), 0:n); 

This creates an array for values ​​from 0 to n with the first and last value doubled.

0
source

To expand the link hiding in @RodyOldenhuis's answer, see Functional Programming Constructs . This package provides a fairly complete set of functional designs. The author also made a series of guest posts on the Art of MATLAB blog , which has many details behind this library. If you do not want to pull the entire package into your project, blog posts also include anonymous one-line elements of some features in the package.

0
source

Source: https://habr.com/ru/post/948523/


All Articles