MATLAB: Indexing cellarrays of varying lengths in a cellarray

cellarrayindexingslicing

Good Day!
Following Problem:
> Given:
% CellArray caCA of N CellArrays CAi of varying sizes >= I
caCA = {CA1, ... CAN};
> Wanted per most efficient way imaginable:
% CellArray of N elements CAi{I}
n_caCA = {CA1{I}, ... CAN{I}};
> Thoughts:
n_caCa = cellfun(@(c) c{I}, caCa);
I'd rather slice my way through this problem, though I'm not sure if it's possible…

Best Answer

Are ther more performant ways to do so?
A loop may be slightly faster as it doesn't have the overhead of the anonymous function call:
n_caCA = zeros(size(caCA));
for idx = 1:numel(caCA)
n_caCA(idx) = caCA{idx}(I);
end
actual performance gain to be tested.
Is it possible to use Matlabs slicing syntax to do so?
No, the matrices in each cell of the cell array may not be stored contiguously in memory, which would be a requirement for slicing. The only way you could perform slicing is by converting the cell array into a matrix, which obviously can't be done if the matrices in the cells are different sizes. Unless of course, you crop the larger matrices to the size of the smallest, but then you'll first have to spend time cropping the matrices.