
238 Accelerating MATLAB
®
Performance
i = repmat(i, [1 s_b(2)]);
j = repmat(j, [s_a(1) 1]);
% Perform a transpose on B by switching dimensions 1 and 2.
b = permute(b, [2 1 3:ndims(b)]);
% Set up P, Q matrices such that when we compute the dot product,
% we obtain the matrix-wise product of A, B across all dimensions
p = a(i(:), r, :);
q = b(j(:), r, :);
c = reshape(dot(p, q, 2), [s_a(1) s_b(2) s_a(3:end)']);
end
5.2.4 Functions That Apply Another Function in a Vectorized Manner
5.2.4.1 arrayfun, cellfun, spfun, and structfun
As extensive as MATLAB’s list of built-in vectorized functions is, it is sometimes difcult to
nd a combin ...