Search code examples
arraysmatlabcell

Concatenate cell arrays


I wish to concatenate two cell arrays together. I have two matrix with different sizes, and from what I understand the only possible way to concatenate them together is to use cell arrays. Here is my code

M = magic(3);
B = {magic(3) 'sip' magic(4) magic(3) }

C = {B; ...
        B; ...
        B; ...
        B}


c1 = C{1}{1,1};
c2 = C{1}{1,3};
c{1} = c1; % after extracting matrix from cell array put it it
c{2} = c2; % into another cell array to attempt to concatenate
conca = [c{1};c{2}]; %returns error.

I'm getting the following error:

??? Error using ==> vertcat
CAT arguments dimensions are not
consistent.

Error in ==> importdata at 26
conca = [c{1};c{2}]; %returns error.

Solution

  • I assume this is your desired output:

    conca = 
    
        [3x3 double]
        [4x4 double]
    

    Where conca{1} is:

     8     1     6
     3     5     7
     4     9     2
    

    and conca{2} is:

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1
    

    You were actually very close. All you need to is change the square braces to curly braces. Like this:

    conca = {c{1};c{2}};
    

    I actually do not get why you have used C and not just did

    conca = {B{1};B{3}}
    

    That will give you the same cell array.