For loop within for loop

3 visualizaciones (últimos 30 días)
DARLINGTON ETAJE
DARLINGTON ETAJE el 8 de Ag. de 2019
Respondida: Jos (10584) el 9 de Ag. de 2019
Please help me out. This code isn't working...
qq=8:10008;
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq;
end
end
I just want answer for A
  3 comentarios
DARLINGTON ETAJE
DARLINGTON ETAJE el 8 de Ag. de 2019
You just solved the problem...qq(n) works....thank you.
Alex Mcaulley
Alex Mcaulley el 9 de Ag. de 2019
Editada: Alex Mcaulley el 9 de Ag. de 2019
You can do it without loop. For example:
qq = 8:10008;
n = 1:10001;
m = 1:9;
[X,Y] = meshgrid(qq + n,m)
A = X + Y;
If you use the loops, at least preallocate your array A to minimize the execution time:
qq = 8:10008;
A = zeros(9,10001);
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq(n);
end
end

Iniciar sesión para comentar.

Respuestas (1)

Jos (10584)
Jos (10584) el 9 de Ag. de 2019
In recent ML versions there is no need for meshgrid or so. The plus syntax will expand the vectors :-)
% a smaller example
n = 1:11
m = 1:4
q = 8:18
A = m' + n + q

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