4

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.

2 Answers 2

5

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.

1
  • Thanks! I'm trying to do something similar on another set of data. Just made a small example which was similar to see it working.
    – Mike Smith
    Feb 28, 2013 at 14:04
4

c{1} refers to the content of a cell, i.e. a matrix in your case. [a b] concatenates the enclosed content, i.e. two matrices (if of the same number of rows).

To concatenate two cell arrays, refer to them as such. To refer to single cells of a cell array, you can use (), e.g. c(1). Thus,

[c(1) c(2)]

works (or [c(1);c(2)]), but for this example,

c(1:2)

is preferable (or c(1:2)' for a column instead of a row).

5
  • I don't think this works since the arrays are different sizes? See HebeleHododo's answer.
    – Floris
    Feb 28, 2013 at 15:26
  • @Floris What does not work? I do not see a hint in HebeleHododo's answer why what the OP tried did not work, or why what I suggest should not work. (btw: both answers were upvoted about the same time, so I guess the OP tried both suggestions and found them both helpful.)
    – arne.b
    Mar 1, 2013 at 8:35
  • When the two arrays are not the same size, as in OP's case, you concatenate them with {} instead of [] - that's where the two answers differ, and yours would not work.
    – Floris
    Mar 1, 2013 at 10:07
  • @Floris Which version of MATLAB did you use? I have a hard time imagining that this ever did not work -- see the reasoning above. Or do you mean the hypothetical in the second sentence? The remark in parenthesis was meant to explain why the OP's approach did not work: not the same number of rows (number of columns does not matter, so technically, even a and b there can be of different size in one dimension).
    – arne.b
    Mar 1, 2013 at 10:54
  • I guess we were reading the question differently. When OP said "matrices of different values", I thought showing how to cope with that was the right approach. Concatenating as you did might technically not generate an error in the exact code as written but it seemed clear to e OP wanted an answer dealing with such things as magic(3) and magic(4), which would be different in both rows and columns. Probably not worth elaborating further...
    – Floris
    Mar 1, 2013 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.