Split array into some equal and some unequal length sections
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Phil Roberts
el 27 de Nov. de 2018
Respondida: Bruno Luong
el 27 de Nov. de 2018
Hello,
I have an array which is 41683 rows long. I wish to split this into as many new arrays of 60 rows as possible.
However, 41683 does not divide by 60 into whole numbers (694.717); hence I need to write some code that will result in 694 columns x 60 rows and a final columns for the remaining 43 rows (0.717 x 60).
I have tried using:
a = [1:41683]'
b = reshape(a,60,[]) but because 41863 is not completely divisible by 60, I get an error.
Can anyone help please?
Many thanks,
Phil
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 27 de Nov. de 2018
Editada: Andrei Bobrov
el 27 de Nov. de 2018
n = 60;
ii = (1:numel(a))';
out = accumarray([rem(ii-1,n)+1,ceil(ii/n)],a,[],[],nan);
or
out = reshape([a;nan(mod(-numel(a),n),1)],n,[]);
0 comentarios
Más respuestas (2)
Bruno Luong
el 27 de Nov. de 2018
a = [1:41683]'
n = size(a,1);
blk = 60;
lgt = ones(1,floor(n/blk))*blk;
r = n-sum(lgt);
if (r > 0) lgt(end+1) = r;
b = mat2cell(a,lgt,1)
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!