No, it is not more efficient, and in some cases may even perform worse. arrayfun
is basically just syntactic sugar for a for
loop. Matlab's JIT is able to optimize "in-place" modifications of preallocated arrays. I have not encountered any cases where arrayfun()
used with a function handle performs better than a for
loop and preallocated arrays on recent-ish versions of Matlab.
In your particular case, they're likely to perform about the same. So use whichever style you prefer.
Note that, strictly speaking, arrayfun
does not perform "vectorized" operations. "vectorized" refers to some built-in Matlab facility that is able to operate on a whole array inside the Matlab engine, using . arrayfun
isn't able to magically do that.
Exception: if you are using the Matlab Parallel Computing Toolbox, or are operating on tall
arrays, then arrayfun
actually can parallelize operations across multiple array elements, and may go faster than a for loop.