avoiding nested for loops for matrix assignment

3 views (last 30 days)
JAMMI ASHOK
JAMMI ASHOK on 15 Oct 2020
Commented: KSSV on 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

Accepted Answer

KSSV
KSSV on 15 Oct 2020
Edited: KSSV on 15 Oct 2020
m = 10000 ; n = 10000 ;
[i,j] = meshgrid(1:n,1:m) ;
a = i+j ;
  4 Comments
KSSV
KSSV on 15 Oct 2020
If you are using a loop and filling the values into matrix......you must initilaize the matrix before loop.

Sign in to comment.

More Answers (1)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by