indexing cellarrays of varying lengths in a cellarray

1 view (last 30 days)
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...
  2 Comments
Guillaume
Guillaume on 16 Aug 2019
You seem to have answered your own question, so I'm not sure what you're asking.
Of course, this will only work for I <= min(cellfun(@numel, caCA))
SuMaSLo
SuMaSLo on 16 Aug 2019
My Questions are:
> Are ther more performant ways to do so?
> Is it possible to use Matlabs slicing syntax to do so?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 16 Aug 2019
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.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!