Divide/spl​it/reshape​/convert a 1x6 cell array with different sizes cell arrays to a 19x1 cell array

Hi
I have a 1x6 cell that is:
Columns 1 through 6
[2x1195 double] [4x1195 double] [2x1195 double] [4x752 double] [4x694 double] [3x695 double]
and I need to convert it to a 19x1 cell that is:
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x1195 double]
[1x752 double]
[1x752 double]
[1x752 double]
[1x752 double]
[1x694 double]
[1x694 double]
[1x694 double]
[1x694 double]
[1x695 double]
[1x695 double]
[1x695 double]
Meaning each row of a "inner" cell becomes the row of the "outer" cell. The first two rows are the 2 rows of Column 1, and so on.
I have already tried the function reshape, cell2mat, mat2cell, cat, but I haven't been able to make it work.
Thank you

Answers (1)

C = ... % Your input
D = cell(size(C))
for k = 1:numel(C)
D{k} = num2cell(C{k}, 2); % [EDITED, {k} added]
end
Result = cat(1, D{:});

3 Comments

It didn't quite work. I think the for loop doesn't have the correct brackets {},() for D. As is, D is only a 3x1 cell with 3 rows of 1x695, meaning it only contains the data for Column 6 of the original 1x6 cell. Instead of 1x6 cell with each column:
Columns 1 through 6
[1x1195 double] [1x1195 double] [1x1195 double] [1x752 double] [1x694 double] [1x695 double]
[1x1195 double] [1x1195 double] [1x1195 double] [1x752 double] [1x694 double] [1x695 double]
[1x1195 double] [1x752 double] [1x694 double] [1x695 double]
[1x1195 double] [1x752 double] [1x694 double]
Also, the Result didn't work because it created a 3x695 double array instead of a 19x1 cell. For the code you sent, D looks more like what I need for the result but for the total 19 rows.
I got it to work as:
C = my cell;
D = cell(size(C));
for k = 1:numel(C)
D{1,k} = num2cell(C{k}, 2);
end
Result = cat(1, D{:});
Thanks
Possibly a cleaner version of the above:
D = cellfun(@(c) num2cell(c, 2), C, 'UniformOutput', false);
Result = vertcat(D{:});

Sign in to comment.

Categories

Asked:

on 16 Nov 2016

Edited:

Jan
on 21 Nov 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!