avoiding nested for loops for matrix assignment

1 vue (au cours des 30 derniers jours)
JAMMI ASHOK
JAMMI ASHOK le 15 Oct 2020
Commenté : KSSV le 15 Oct 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

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 15 Oct 2020
Modifié(e) : Matt J le 15 Oct 2020
x = 1:10000;
a = x.' + x;

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by