MATLAB: Find value in a cell array if you have its index

cell arrays

I have a cell array which has vectors as cells.I have an array containing some indices.I need from that indices to find the corresponding values of the cell array.For example
A{1,1}=[35,9,45,7];
A{2,1}=[21,4,65,3,11];
A{3,1}=[14,32];
and
B=[2;5;1];
I need to get
C=[9;11;14]
Which is the faster way?
my suggestion is
for ia=1:lenght(A)
C=A{ia}(B(ia));
end

Best Answer

n = cellfun(@numel,A);
ii = cumsum(n);
x = [A{:}];
out = x(B+[0;ii(1:end-1)]);