How to Loop through a matrix/array
Mostrar comentarios más antiguos
I have a matrix/array that is 804 X 1. I want to be able to use a for loop to take the first 58 values put them in array a1, then take the next 58 values and place them in array a2 and so on. How would I do this?
Respuestas (2)
Azzi Abdelmalek
el 11 de Mzo. de 2013
If you want a3 just use
n=3
idx1=(n-1)*58+1
idx2=n*58
out=a(idx1:idx2)
1 comentario
Adam
el 11 de Mzo. de 2013
Azzi Abdelmalek
el 11 de Mzo. de 2013
Editada: Azzi Abdelmalek
el 11 de Mzo. de 2013
a=rand(1,804) %example
m=numel(a);
n=ceil(m/58);
r=n*58-m;
for k=1:n-1
idx1=(k-1)*58+1;
idx2=k*58;
assignin('base',sprintf('a%d',k),a(idx1:idx2))
end
assignin('base',sprintf('a%d',k+1),a(idx2+1:end))
1 comentario
Adam
el 11 de Mzo. de 2013
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!