Search code examples
matlabmatrixcell-array

concatenate cell-array and matrix in Matlab


I have a matrix m (16x3 double) and a cell array c (16x13 cell). I want to create a cell array S 16x16, so with the same number of rows, but appending the columns of c to the columns of m.

I tried:

S= {m,c};

but the results is S{1,1,} = 16x3 double, and S{1,2} = 16x13 cell.

I also tried

S = horzcat(m,c);

but it gives me an error:

Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Any hint? Thank you!


Solution

  • You have to convert your matrix to a cell first:

    S=[num2cell(m),c]
    

    The [a,b] used here is just a short way of writing horizcat(a,b)