Applying a function on array that returns outputs with different size in a vectorized manner
- 阅读更多 关于 Applying a function on array that returns outputs with different size in a vectorized manner
问题 How to apply a function that returns non scalar output to arrays using arrayfun ? For example - How to vectorize the following code? array = magic(5); A = cell(size(array)); for i=1:5 for j=1:5 A{i,j} = 1:array(i,j); end end This naive attempt to vectorize does not work, because the output is not a scalar array = magic(5); result = arrayfun(@(x)(1:x),array); 回答1: There are 2 methods to achieve it: It is possible to set 'UniformOutput' to false. Then, the result is a cell array. result =