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,
채택된 답변
추가 답변 (3개)
Image Analyst
2015년 12월 18일
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
Luis Isaac
2015년 12월 18일
Josh
2024년 5월 4일
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
Fletcher Garrison
2021년 2월 3일
편집: Fletcher Garrison
2021년 2월 3일
You can also use:
Arrcell = {1,2,3,4,5};
new = magic(5);
Arrcell = horzcat(new,Arrcell);
For future people looking this up:
testCell = {"one", 2, 10, "llama"}
testCell = {"newInput", testCell{:}}
댓글 수: 1
I stand corrected by MATLAB autofix. Apparently this is faster.
testCell ={"one", 2, 10, "llama"}
testCell = [{"newInput"}, testCell(:).']
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!