MATLAB: index a cell array with cell array of arrays and return a cell array

Here are two alternative solutions:

  • Collect all the indices of B together with the function cell2mat, index the contents of A to make one large matrix, then divide that matrix up using the function mat2cell and the sizes of the index arrays in B:

    N = size(A{1});                        % Size of an array in A
    M = cellfun('prodofsize', B);          % Array of sizes of elements in B
    C = mat2cell([A{cell2mat(B)}], N, M);
    
  • Here's a more compact version of your cellfun-based solution:

    C = cellfun(@(x) {[A{x}]}, B);
    

Ultimately, I would decide what solution to use based on speed and readability, which may actually turn out to be your for-loop-based solution.