splitting Cell array in a loop

3 visualizaciones (últimos 30 días)
HYZ
HYZ el 29 de Ag. de 2022
Comentada: Stephen23 el 30 de Ag. de 2022
Hi,
I want to split X into Y, a 1 x 4 cell. Y will be {[1:8]} {[9:16]} {[17:24]} {[25 32]}. thanks.
X = 1:32;
Y = cell (1,4);
for j = [1,9,17,25];
for i = 1: 4
Y{1,i} = X (j:j+7);
end
end

Respuesta aceptada

Voss
Voss el 29 de Ag. de 2022
X = 1:32;
Y = cell(1,4);
j = [1,9,17,25];
for i = 1:numel(j)
Y{1,i} = X(j(i):j(i)+7);
end
disp(Y)
{[1 2 3 4 5 6 7 8]} {[9 10 11 12 13 14 15 16]} {[17 18 19 20 21 22 23 24]} {[25 26 27 28 29 30 31 32]}
Or:
Y = num2cell(reshape(X,[],4).',2).'
Y = 1×4 cell array
{[1 2 3 4 5 6 7 8]} {[9 10 11 12 13 14 15 16]} {[17 18 19 20 21 22 23 24]} {[25 26 27 28 29 30 31 32]}

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by