0
votes

I have a cell array of matrices which look like this:

G = 
Columns 1 through 6
    [507x1 double]    [255x1 double]    [130x1 double]    [67x1 double]    [36x1 double]    [19x1 double]

Columns 7 through 9
    [11x1 double]    [6x1 double]    [4x1 double]

This means that the matrices are inside a cell array. Matrix G has a total of 507+255+130+67+36+19+11+6+4=1035 elements in it. I can extract any cell by using the G{i} command. However, I need all of these 1035 elements in single column or row matrix. I have tried cell2mat(G) but that doesn't work.

What can I do here to create a single matrix that contains all of these other matrices together?

1
In what way does cell2mat not work? This Gist functions as expected. - excaza

1 Answers

2
votes

Use cat for that and concatenate vertically, or along the first dimension:

G = cat(1, G{:});

The syntax G{:} produces what is known as a comma-separated list. This would be equivalent to you doing:

G = cat(1, G{1}, G{2}, ..., G{9});

Instead of you having to type in all of the individual cell elements to be used for concatenation, doing G{:} unpacks all of that for you.

Minor Note

cell2mat essentially performs cat in the fashion that I talked about above. If you look at the source for cell2mat, it's bloated with a lot of sanity checks, but the heart of the function is simply using cat and taking advantage of comma-separated lists. I don't know why cell2mat wouldn't work for you.