avoiding nested for loops for matrix assignment

4 visualizaciones (últimos 30 días)
JAMMI ASHOK
JAMMI ASHOK el 15 de Oct. de 2020
Comentada: KSSV el 15 de Oct. de 2020
Hi,
I am a beginner to matlab. Can you please tell me how to avoid using nested for loops in matlab.
THis piece of code is taking lot of space in my hard drive and time.
for i = 1:10000
for j = 1:10000
a(i,j) = i+j
end
end

Respuesta aceptada

KSSV
KSSV el 15 de Oct. de 2020
Editada: KSSV el 15 de Oct. de 2020
m = 10000 ; n = 10000 ;
[i,j] = meshgrid(1:n,1:m) ;
a = i+j ;
  4 comentarios
JAMMI ASHOK
JAMMI ASHOK el 15 de Oct. de 2020
Yes. If I am initializing it is improving the speed.
KSSV
KSSV el 15 de Oct. de 2020
If you are using a loop and filling the values into matrix......you must initilaize the matrix before loop.

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 15 de Oct. de 2020
Editada: Matt J el 15 de Oct. de 2020
x = 1:10000;
a = x.' + x;

Categorías

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

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by