简体   繁体   中英

Integrating matrix vectors into a cell array using MATLAB?

I have this cell array of matrices:

a = [14x16 double] [14x17 double] [14x27 double][14x62 double] [14x16 double]

Unfortunately, I don't want that. What I need is to get all the vectors in the matrix and get a large cell array of vectors.

How can I achieve this?

I need an array of size 16+17+27+62+16 where every vector has 14 elements.

I haven't used matlab a lot before, I am sure this is sort of trivial. Can someone help?

You can call num2cell on each of the cell arrays, and then concatenate them all using cat .

%// Generate some test data
a = arrayfun(@(x)rand(14, x), [16 17 27 62 16] , 'uni', 0);

    [14x16 double]  [14x17 double]  [14x27 double]  [14x62 double]  [14x16 double]

%// Now merge them into a cell array of vectors
newcell = cellfun(@(x)num2cell(x, 1), a, 'uni', 0);
newcell = cat(2, newcell{:});

And just to verify that everything is the dimension we expect

isequal(size(newcell), [1 16+17+27+62+16])

    1

isequal(size(newcell{1}), [14 1])

    1

If instead you simply want a large matrix, you can just concatenate the initial data into a matrix (along the second dimension):

matrix = cat(2, a{:});

size(matrix)

    14   138

I would recommend the matrix approach as opposed to the cell array approach as MATLAB is highly optimized to perform operations on matrices. You will definitely take a performance hit using cell arrays instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM