avoiding nested for loops for matrix assignment

2 次查看(过去 30 天)
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

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2020-10-15
编辑:Matt J 2020-10-15
x = 1:10000;
a = x.' + x;

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by