indexing cellarrays of varying lengths in a cellarray

1 visualización (últimos 30 días)
SuMaSLo
SuMaSLo el 16 de Ag. de 2019
Respondida: Guillaume el 16 de Ag. de 2019
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 comentarios
Guillaume
Guillaume el 16 de Ag. de 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 el 16 de Ag. de 2019
My Questions are:
> Are ther more performant ways to do so?
> Is it possible to use Matlabs slicing syntax to do so?

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 16 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by