Borrar filtros
Borrar filtros

extracting values from embedded for loops

2 visualizaciones (últimos 30 días)
darksideofthemoon101
darksideofthemoon101 el 7 de Abr. de 2011
Hi,
I have one loop embedded inside another and want to extract the innermost value for each change in both the inner loop and outer loop. My code is analogous to (but much more complex than):
for x=1:1:M
for y=1:1:N
z=x*y;
answer1(y)=z;
end
end
The line 'answer1(y)' will create an array with N elements, each corresponding to the given y. However if i put a similar line in the outer for loop I get a dimension error. Any suggestions?
Thanks,
Richard

Respuesta aceptada

Arnaud Miege
Arnaud Miege el 7 de Abr. de 2011
I think what you really should do is something like:
answer1 = zeros(M,N); % pre-allocate variable
for x=1:1:M
for y=1:1:N
z=x*y;
answer1(x,y)=z;
end
end
Then you have all the data from each loop iteration.
HTH,
Arnaud

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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