Matlab list comprehension
Just to add to the good answer of @yoda, instead of using UniformOutput
, you can also use {}
brackets:
res = arrayfun(@(t){foo(t)}, x)
Also, in some occasions, foo
is already vectorized.
x = 1:10;
foo = @(t)(power(t,2));
res = foo(x);
arrayfun
is what you need. For example:
res = arrayfun(@foo, x)
Since foo
always returns a scalar, the above will work and res
will also be a vector of the same dimensions as x
. If foo
returns variable length output, then you will have to set 'UniformOutput'
to false
or 0
in the call to arrayfun
. The output will then be a cell
array.