Can I write the following in a one liner?
x = [1,3,5,7,9]
res = zeros(1,size(x,2));
for i=1:size(x,2);
res(i) = foo(x(i));
end;
Assume that the foo function does not handle arrays as expected. In my case foo returns a scalar even when giving an array as argument.
In Python, for instance, it would look like this:
x = [1,3,5,7,9]
res = [foo(y) for y in x]
arrayfunis what you need. For example:Since
fooalways returns a scalar, the above will work andreswill also be a vector of the same dimensions asx. Iffooreturns variable length output, then you will have to set'UniformOutput'tofalseor0in the call toarrayfun. The output will then be acellarray.