Add first element to a cell array

Dear,
I would like to add a new element in the first position in an cell array For example if ArrCell={[1],[2],[3],[4],[5]} is the existing cell and [new] is a matrix. I would like to get ArrCell={[new],[1],[2],[3],[4],[5]}
Of course, I can get this result using a temporal cell array and looping over all the elements of the existing one, but the quesion is if there an efficient way to get this result
Thanks in advance,

 Respuesta aceptada

Adam
Adam el 18 de Dic. de 2015
ArrCell = [ { new }, ArrCell ];
should work. I'm not at my machine with Matlab at the moment so can't double-check, but fairly sure that works.

5 comentarios

Luis Isaac
Luis Isaac el 18 de Dic. de 2015
Great, It works!!! Thanks a lot,
Guillaume
Guillaume el 18 de Dic. de 2015
Note that there is no data structure matlab that allows for efficient insertion at the head of a container (as you're doing here). So, while the above way is efficient in that you don't have to write much code, it's still inefficient in that matlab has to reallocate a whole new array and copy all the data in the background. So your insertion is O(n).
Of course, it does not matter if your cell array is small enough.
Correct answer would be:
ArrCell = [ { new }; ArrCell ];
Adam
Adam el 20 de Mzo. de 2020
ArrCell was a row array. You can't concatenate vertically a scalar and a 5-element row array.
new = magic(3) % A matrix
ArrCell={[1],[2],[3],[4],[5]} % A cell array
ArrCell = [ { new }, ArrCell ] % Adam uses comma
ArrCell2 = [ { new }; ArrCell ]; % Daniel uses semicolon
new =
8 1 6
3 5 7
4 9 2
ArrCell =
1×5 cell array
{[1]} {[2]} {[3]} {[4]} {[5]}
ArrCell =
1×6 cell array
{3×3 double} {[1]} {[2]} {[3]} {[4]} {[5]}
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in test1 (line 12)
ArrCell2 = [ { new }; ArrCell ];

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 18 de Dic. de 2015
But the big question is why are you even using a cell array for that instead of a simple numerical array. Cell arrays are complicated and use a huge amount of overhead. From what you've shown there is no reason to use one. I recommend this instead
Arr = [1,2,3,4,5];
Arr = [new, 1,2,3,4,5]

2 comentarios

Luis Isaac
Luis Isaac el 18 de Dic. de 2015
I using a cell array because ArrCell is composed of vectors and matrices of different sizes, i.e. size of [new] is different, or could be different, than [1]
Josh
Josh el 4 de Mayo de 2024
appreciate this nudge in a more efficient direction, i fallback on cells arrays out of habit and ease but switching over to numerical arrays where possible...i make animations and cells help when image sizes are changing at least but seem uneccessary when image size is fixed

Iniciar sesión para comentar.

Fletcher Garrison
Fletcher Garrison el 3 de Feb. de 2021
Editada: Fletcher Garrison el 3 de Feb. de 2021
You can also use:
Arrcell = {1,2,3,4,5};
new = magic(5);
Arrcell = horzcat(new,Arrcell);
Jonathan
Jonathan el 18 de Sept. de 2024
For future people looking this up:
testCell = {"one", 2, 10, "llama"}
testCell = 1x4 cell array
{["one"]} {[2]} {[10]} {["llama"]}
testCell = {"newInput", testCell{:}}
testCell = 1x5 cell array
{["newInput"]} {["one"]} {[2]} {[10]} {["llama"]}

1 comentario

I stand corrected by MATLAB autofix. Apparently this is faster.
testCell ={"one", 2, 10, "llama"}
testCell = 1x4 cell array
{["one"]} {[2]} {[10]} {["llama"]}
testCell = [{"newInput"}, testCell(:).']
testCell = 1x5 cell array
{["newInput"]} {["one"]} {[2]} {[10]} {["llama"]}

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 18 de Dic. de 2015

Comentada:

el 18 de Sept. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by