Problem with cell array appending

조회 수: 17 (최근 30일)
Jaya
Jaya 2021년 9월 16일
댓글: Stephen23 2021년 9월 17일
mycell is appended with cell arrays in three different areas of my code. Like below
mycell= { }
mycell= A(:,:,1) %1st time. A(:,:,1) is a 1*5 cell array
mycell= {mycell ; B(:,:,1) } %2nd time. B(:,:,1) is a 1*5 cell array
mycell= {mycell ; C(:,:,1) } %3rd time. C(:,:,1) is a 1*5 cell array
1st time output is OK: mycell is a cellarray of 1*5.
2nd time output is also OK: mycell is a 2*1 cell array with each element of 1*5 size.
BUT 3rd time output: mycell is still a 2*1 cell array as below. Why? Why do the previous two elements form as a single element in this third time? Can someone tell me how do I avoid this?
%the output I get after 3rd time line
mycell =
2×1 cell array
{2×1 cell}
{1×5 cell}
% but the output I want is something like.
{1×5 cell}
{1×5 cell}
{1×5 cell}
  댓글 수: 1
Stephen23
Stephen23 2021년 9월 17일
Note the difference:
  • {} curly braces creates a cell array, where the inputs are nested inside the new cell array.
  • [] square brackets are a concatenation operator. These are used to concatenate any array type.
So if you want to nest cell arrays inside other cell arrays, then use curly braces. But if you want to concatenate any arrays together, use square brackets (or the operators CAT, HORZCAT, VERTCAT).

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2021년 9월 16일
Assigning is likely a more efficient approach than concatenation —
A(:,:,1) = randn(1,5);
B(:,:,1) = randn(1,5);
C(:,:,1) = randn(1,5);
mycell{1,:}= A(:,:,1) %1st time. A(:,:,1) is a 1*5 cell array
mycell = 1×1 cell array
{[0.4460 0.5237 0.2281 0.4874 -1.1267]}
mycell{2,:}= B(:,:,1) %2nd time. B(:,:,1) is a 1*5 cell array
mycell = 2×1 cell array
{[ 0.4460 0.5237 0.2281 0.4874 -1.1267]} {[-1.9246 1.0601 0.0070 -0.5618 -0.8078]}
mycell{3,:}= C(:,:,1) %3rd time. C(:,:,1) is a 1*5 cell array
mycell = 3×1 cell array
{[ 0.4460 0.5237 0.2281 0.4874 -1.1267]} {[-1.9246 1.0601 0.0070 -0.5618 -0.8078]} {[ 0.7868 0.1859 0.6091 1.0478 -0.2925]}
This also allows for preallocation, that can significantly improve code efficiency.
The cell concatenation approach creates ‘cells-of-cells’, making the interpretation more difficult. The MATLAB concatenation operator are the square brackets [] so using them will produce the correct result —
mycell2 = { }
mycell2 = 0×0 empty cell array
mycell2 = {A(:,:,1)} %1st time. A(:,:,1) is a 1*5 cell array
mycell2 = 1×1 cell array
{[0.4460 0.5237 0.2281 0.4874 -1.1267]}
mycell2 = [mycell2 ; {B(:,:,1)} ] %2nd time. B(:,:,1) is a 1*5 cell array
mycell2 = 2×1 cell array
{[ 0.4460 0.5237 0.2281 0.4874 -1.1267]} {[-1.9246 1.0601 0.0070 -0.5618 -0.8078]}
mycell2 = [mycell2 ; {C(:,:,1)} ] %3rd time. C(:,:,1) is a 1*5 cell array
mycell2 = 3×1 cell array
{[ 0.4460 0.5237 0.2281 0.4874 -1.1267]} {[-1.9246 1.0601 0.0070 -0.5618 -0.8078]} {[ 0.7868 0.1859 0.6091 1.0478 -0.2925]}
This is less efficient than the indexing approach, because it precludes preallocation.
Experiment to get different results.
.
  댓글 수: 2
Jaya
Jaya 2021년 9월 16일
Thanks a lot. This solution has made my code interpretation so much easier.
Star Strider
Star Strider 2021년 9월 16일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by