Could anyone help me how to simplify the following code.

A = cell(1,15)
B=A'
B{1}=cell(2,1)
B{2}=cell(2,1)
B{3}=cell(2,1)
B{4}=cell(2,1)
B{5}=cell(2,1)
B{6} = cell(2,2)
B{7}= cell(2,2)
B{8} = cell(2,2)
B{9}= cell(2,2)
B{10} = cell(2,2)
B{11}= cell(2,3)
B{12}= cell(2,3)
B{13}= cell(2,3)
B{14}= cell(2,3)
B{15}= cell(2,3)
in this code instead to write each and every row of the cell array separately is there any method that could be done to simplify it in the single step. For example B{1}.......B{5} are of the same size(2,1) ,B{6}.......B{10} of (2,2) , B{11}.....B{15} of (2,3) .IS it possible to write all the lines in a single step.

1 comentario

KSSV
KSSV el 23 de Jun. de 2021
Why you are bothered to initialize a cell in each cell in an cell array? You are missing something here.

Iniciar sesión para comentar.

 Respuesta aceptada

I am not sure why you are creating a nested cell array. However you can do it like this.
B = arrayfun(@(x,y)cell(x,y),ones(15,1)*2,repelem([1:3]',5,1),'UniformOutput',false)
B = 15×1 cell array
{2×1 cell} {2×1 cell} {2×1 cell} {2×1 cell} {2×1 cell} {2×2 cell} {2×2 cell} {2×2 cell} {2×2 cell} {2×2 cell} {2×3 cell} {2×3 cell} {2×3 cell} {2×3 cell} {2×3 cell}

4 comentarios

thanks.
The reason i am creating in this manner is to train the neural network model.
In the previous code I created for 15 rows each 5 rows of different sizes.
If suppose I need to create for 100 rows i.e. B= 100x1 cell array, in which each 5 rows of different sizes
lets say, B{16}.......B{20} of (2x4) B{21}....B{25} of (2x5)........B{96}.......B{100} of (2x17).
Could you please help me how to rewrite
B = arrayfun(@(x,y)cell(x,y),ones(15,1)*2,repelem([1:3]',5,1),'UniformOutput',false)
for the above statement.
ok i tried I got the result for 100x1 cell array. Thanks for your help.
No problem.
I need another help from you.
could you please help me to create categorical array for B = 15×1 cell array
{2×1 cell}
{2×1 cell}
{2×1 cell}
{2×1 cell}
{2×1 cell}
{2×2 cell}
{2×2 cell}
{2×2 cell}
{2×2 cell}
{2×2 cell}
{2×3 cell}
{2×3 cell}
{2×3 cell}
{2×3 cell}
{2×3 cell}

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 23 de Jun. de 2021

Comentada:

el 23 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by