1

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 Answers 2

2

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}
Sign up to request clarification or add additional context in comments.

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.