1
votes

If I create a cell array using:

clear all
data = {rand(1,5),rand(1,4),rand(1,4),rand(1,6)};
a = cell(1,length(data));

how is it then possible to create a cell array in each cell of a which is the same length as the corresponding cell in data. I know this can easily be done using a loop, but how would it be possible by using the cellfun function?

2

2 Answers

2
votes

Do you want something like that?

data = {rand(1,5),rand(1,4),rand(1,4),rand(1,6)};
a2=cellfun(@(x) cell(size(x)),data,'UniformOutput',0)
a2 = 
    {1x5 cell}    {1x4 cell}    {1x4 cell}    {1x6 cell}
0
votes

You could also accomplish this by using CELLFUN to just get the sizes of each cell, create all the cells you need, then divide them up using MAT2CELL:

>> cellSizes = cellfun('size',data,2);
>> a = mat2cell(cell(1,sum(cellSizes)),1,cellSizes)

a = 

    {1x5 cell}    {1x4 cell}    {1x4 cell}    {1x6 cell}