Filter löschen
Filter löschen

avoiding nested for loops for matrix assignment

2 Ansichten (letzte 30 Tage)
JAMMI ASHOK
JAMMI ASHOK am 15 Okt. 2020
Kommentiert: KSSV am 15 Okt. 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 15 Okt. 2020
Bearbeitet: Matt J am 15 Okt. 2020
x = 1:10000;
a = x.' + x;

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by