avoiding nested for loops for matrix assignment

4 ビュー (過去 30 日間)
JAMMI ASHOK
JAMMI ASHOK 2020 年 10 月 15 日
コメント済み: KSSV 2020 年 10 月 15 日
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;
  1 件のコメント
JAMMI ASHOK
JAMMI ASHOK 2020 年 10 月 15 日
Thanks Matt - it is working.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by