Concatenate Cell Arrays

I want to combine two cells of an array together. I have two matrices of different sizes, and from what I understand, the only possible way to combine 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 get the following error:

 ??? Error using ==> vertcat CAT arguments dimensions are not consistent. Error in ==> importdata at 26 conca = [c{1};c{2}]; %returns error. 
+4
source share
2 answers

I assume this is your desired result:

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

Where conca{1} :

  8 1 6 3 5 7 4 9 2 

and conca{2} :

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

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

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

Actually, I don’t understand why you used C , and not just

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

This will give you the same array of cells.

+5
source

c{1} refers to the contents of the cell, that is, to the matrix in your case. [ab] combines the enclosed content, i.e. two matrices (if the same number of rows).

To combine two arrays of cells, refer to them as such. To refer to individual cells in an array of cells, you can use () , for example. c(1) . In this way,

 [c(1) c(2)] 

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

 c(1:2) 

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

+4
source

All Articles