Use if argument in arrayfun in Octave / Matlab
2 answers
In Octave, you cannot use the if / else statements of an inline or anonymous function in the usual way. You can define your function in your own file or as a subfunction like this:
function a = testIf(x) if x>=2 a = 1; else a = 0; end end and call arrayfun as follows:
arrayfun(@testIf,a) ans = 0 1 1 1 Or you can use this work with a built-in function:
iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, ... 'first')}(); arrayfun(iif, a >= 2, 1, true, 0) ans = 0 1 1 1 More information here .
+6