matrix - Use if clause in arrayfun in Octave/Matlab -
is possible use "if" in arrayfun following in octave?
a = [ 1 2; 3 4]; arrayfun(@(x) if x>=2 1 else 0 end, a)
and octave complains:
>>> arrayfun(@(x) if x>=2 1 else 0 end, a) ^
is if clause allowed in arrayfun?
in octave can't use if/else statements in inline or anonymous function in normal way. can define function in it's own file or subfunction this:
function = testif(x) if x>=2 = 1; else = 0; end end
and call arrayfun this:
arrayfun(@testif,a) ans = 0 1 1 1
or can use work around inline function:
iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, ... 'first')}(); arrayfun(iif, >= 2, 1, true, 0) ans = 0 1 1 1
there's more information here.
Comments
Post a Comment